java 7+ 文件创建

标签 java file io nio

我正在尝试在我知道我有权限的目录中创建一个新文件。我不在乎这个文件在创建时是否存在,因为我不介意覆盖。我正在尝试为此使用 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)

最佳答案

我认为你必须做一些检查。

  1. 检查该目录是否存在,因为如果不存在,则不会自动为您创建。
  2. 如果您尝试创建的文件存在,createFile() 方法将引发异常,因此您必须检查它并使用现有文件或删除现有文件并创建新文件.

因此,如果我想根据您的要求创建一个新文件,我会执行以下操作:

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);

关于java 7+ 文件创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39419826/

相关文章:

python - 在文件命令 Python 中添加循环

File.length() 的 Android 资源替代方案?

MYSQL TINYBLOB和LONGBLOB

file - GTFS 有哪些问题?

c++ - 为什么不包含 iostream 头文件?

java - 读取具有特定格式的文本文件

java - 在 ListView 中编辑文本?

java - JFreeChart - 在 CombinedDomainXYPlot 中显示/隐藏子图

java - 在java中获取距离大坝 "Event"的天数

java - IntelliJ 在输入期间编译时将 .class 文件放在哪里