0

I am trying to create a new file to a directory I know I have permissions for. I don't care if this file exists at the time of creation or not as I don't mind overwriting. I am trying to use nio library for this one but continually am getting NoSuchFileException. In this case, there is no file or directory because I am trying to do this for the first time. I was under the impression the new Files library would handle all of the overwriting or creation for me. I'd like to write to this file after this works. What am I missing here?

    public static void createTextFile(String fileName)throws IOException{

    //Member variables
    String str_path = "C:\\reports\\errors\\";

    Path dir = Paths.get(str_path);
    Path errorFile = dir.resolve(fileName + ".txt");
    Files.createFile(errorFile);
}

Exception I keep getting:

java.nio.file.NoSuchFileException: C:\reports\errors\alert_exception.txt
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.createFile(Unknown Source)
at utilities.Utils.createTextFile(Utils.java:267)
at listeners.EventHandler.onException(EventHandler.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver$1.invoke(EventFiringWebDriver.java:80)
at com.sun.proxy.$Proxy6.onException(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:105)
at com.sun.proxy.$Proxy7.get(Unknown Source)
at org.openqa.selenium.support.events.EventFiringWebDriver.get(EventFiringWebDriver.java:163)
at release.SampleTest.ExampleForAlert(SampleTest.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:112)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:176)
6
  • 2
    Does the directory C:\reports\errors exist? Commented Sep 9, 2016 at 21:18
  • 2
    Instead of being under wrong impressions, you should read the javadocs which tell you what methods actually do. Commented Sep 9, 2016 at 21:21
  • Oh sorry. I needed to add a bit of extra info. I need to assume the directories will need to be made from scratch every time. I dont care if they're not there I need to make fresh ones. Commented Sep 9, 2016 at 21:23
  • @Kayaman I did. And it still isn't working. So obviously the impression I am under is wrong. Commented Sep 9, 2016 at 21:24
  • 1
    @JoeC Throws IOException - if an I/O error occurs or the parent directory does not exist Commented Sep 9, 2016 at 21:27

1 Answer 1

1

I think you have to do a couple of checks.

  1. Check if the directory exists because if it doesn't exist it will not be created for you automatically.
  2. If the file you are trying to create exists the createFile() method will throw an exception so you have to check it and either use the existing file or delete the existing file and create a new one.

So the following is what I would do if I want to create a new file according to your requirement:

String str_path = "C:\\reports\\errors\\";
Path dir = Paths.get(str_path);
if (Files.notExists(dir)) {
    dir = Files.createDirectory(dir);
}
Path errorFile = dir.resolve(fielname + ".txt");
Files.deleteIfExists(errorFile);
Files.createFile(errorFile);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.