【发布时间】:2016-09-09 21:17:00
【问题描述】:
我正在尝试在我知道我有权访问的目录中创建一个新文件。我不在乎这个文件在创建时是否存在,因为我不介意覆盖。我正在尝试为此使用 nio 库,但不断收到 NoSuchFileException。在这种情况下,没有文件或目录,因为我是第一次尝试这样做。我的印象是新的文件库会为我处理所有的覆盖或创建。我想在这个工作之后写入这个文件。我在这里错过了什么?
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);
}
我不断收到的异常:
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)
【问题讨论】:
-
目录
C:\reports\errors是否存在? -
不要有错误的印象,你应该阅读javadocs,它告诉你什么方法实际上是做什么的。
-
抱歉。我需要添加一些额外的信息。我需要假设每次都需要从头开始制作目录。我不在乎他们是否不在那里,我需要制作新鲜的。
-
@Kayaman 我做到了。它仍然无法正常工作。所以很明显我的印象是错误的。