【问题标题】:NoSuchFileException when creating a file using nio使用 nio 创建文件时出现 NoSuchFileException
【发布时间】:2015-06-09 14:58:18
【问题描述】:

我正在尝试使用 java nio 创建一个新文件,但我遇到了 createFile 错误。错误如下所示:

 createFile error: java.nio.file.NoSuchFileException: /Users/jchang/result_apache_log_parser_2015/06/09_10:53:49

代码段如下所示:

 String filename = "/Users/jchang/result_apache_log_parser_" + filename_date;
        Path file = Paths.get(filename);
        try {
            Files.createFile(file);
        } catch (FileAlreadyExistsException x) {
            System.err.format("file named %s" +
                    " already exists%n", file);
        } catch (IOException x) {
            System.err.format("createFile error: %s%n", x);
        }

有人知道如何解决这个问题吗?感谢您的帮助!

【问题讨论】:

  • Files.create(...) 只创建文件,而不是文件夹。这可能是您的问题还是该文件夹存在?
  • 哦,天哪,我需要最后的文件类型吗?例如.txt?
  • 不,你没有。但是文件的路径(即文件所在的文件夹)必须存在。
  • 好的,它肯定存在,它只是我要链接到的主目录。
  • Files.createDirectories(file.getParent()); - 你确定用户是用大写的 U 写的吗?

标签: java file-io nio


【解决方案1】:

我会说 Turing85 是正确的。您的 filename_date 变量中有斜线。所以/Users/jchang/result_apache_log_parser_2015 必须作为目录存在。这就是NoSuchFileException 丢失目录的原因。

【讨论】:

  • 但是还有一个问题(至少在windows下):冒号是非法字符。我正在研究解决方案
  • 没错,我还建议将filename_date 中的一些字符替换为有效字符。例如,filename_data = filename_date.replace("/", "_").replace(":", "_");
  • 太棒了,解决了它。谢谢。
【解决方案2】:

您的代码至少有两个问题。首先:您的文件名中有路径分隔符 (/)。第二:至少在 Windows 下,您的解决方案在 filname (:) 中有非法字符。

要摆脱第一个问题,您可以走两条路线:a) 创建您需要的所有文件夹或 b) 将分隔符更改为不同的内容。我会解释两者。

要创建一个路径的所有文件夹,你可以简单地调用

Files.createDirectories(path.getParent());

path 是一个文件(重要!)。通过在文件中调用getParent(),我们得到了path 所在的文件夹。 Files.createDirectories(...) 负责其余的工作。

b) 更改分隔符:没有比这更简单的了:

String filename =  "/Users/jchang/result_apache_log_parser_"
                 + filename_date.replace("/", "_")
                                .replace(":", "_");

这应该产生类似/User/jchang/result_apache_parser_2015_06_09_10_53_29

对于 b),我们也解决了第二个问题。

现在让我们一起设置并应用nio 的一些小技巧:

String filename =  "/Users/jchang/result_apache_log_parser_"
                 + filename_date.replace('/', '_')
                                .replace(':', '_');

Path file = Paths.get(filename);
try {
    // Create sub-directories, if needed.
    Files.createDirectories(file.getParent());
    // Create the file content.
    byte[] fileContent = ...;
    // We do not need to create the file manually, let NIO handle it.
    Files.write(file
                , fileContent
                // Request permission to write the file
                , StandardOpenOption.WRITE
                // If the file exists, append new content
                , StandardOpenOption.APPEND
                // If the file does not exist, create it
                , StandardOpenOption.CREATE);
} catch (IOException e) {
    e.printStackTrace();
}

有关nio的更多信息,请单击here

【讨论】:

    【解决方案3】:

    正如许多人所说,您需要创建中间目录,例如 ../06/..

    所以使用这个,在创建文件之前创建不存在的目录,

    Files.createDirectories(mPath.getParent());
    

    所以你的代码应该是:

        Path file = Paths.get(filename);
        try {
            Files.createDirectories(file.getParent());
            Files.createFile(file);
        } catch (FileAlreadyExistsException x) {
            System.err.format("file named %s" +
                    " already exists%n", file);
        } catch (IOException x) {
            System.err.format("createFile error: %s%n", x);
        }
    

    【讨论】:

    • 为我工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 2019-04-29
    • 2014-03-24
    • 1970-01-01
    相关资源
    最近更新 更多