【问题标题】:Files.createDirectory() : FileAlreadyExistsExceptionFiles.createDirectory() : FileAlreadyExistsException
【发布时间】:2013-04-23 21:01:29
【问题描述】:

我在使用 Java 7 的 Files 类时遇到了一个看似奇怪的问题。 我想在开始写作之前确保我的目录和文件存在以避免FileNotFoundException,并根据JavadocscreateDirectory 检查“文件是否存在,如果不存在则创建目录存在”

那么如果它先检查,为什么在目录已经存在的情况下我下面的代码会有问题?

private void writeFile() throws IOException {
    // Make sure parent directory and file are ready
    File file = "mydirectory/my.file";
    File parent = file.getParentFile();
    if (parent != null)
        Files.createDirectory(parent.toPath()); // Why do I get FileAlreadyExistsException? =[
    Files.createFile(file.toPath());

    // Do some file writing stuff!
}

我知道我可以只做一个“如果文件不存在则创建”的事情,但我认为这种方法的重点是为我处理所有这些!

异常数据:

java.nio.file.FileAlreadyExistsException: mydirectory
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.createDirectory(Unknown Source)
at java.nio.file.Files.createDirectory(Unknown Source)

【问题讨论】:

  • 你能发布异常吗?并告诉我它抛出了哪条线?

标签: java file-io java-7


【解决方案1】:

来自documentation

public static Path createDirectories(Path dir, FileAttribute<?>... attrs) throws IOException

“通过首先创建所有不存在的父目录来创建目录。与 createDirectory 方法不同,如果由于目录已存在而无法创建目录,则不会引发异常。”

也许你可以用那个

【讨论】:

  • 是的,这行得通——但现在createFile 抛出异常;有类似的事情吗?没有createFiles这样的方法。
  • 搞砸了,我将使用 file.exists() 以旧方式进行操作
  • 非常感谢您的回答。但是今天的“java你在跟我开玩笑吗?”片刻。 (最初尝试使用createDirectory)
  • 有人应该提交一份关于这个的 Java 错误报告;它可能不完全是一个错误,但至少甲骨文的人应该更改createDirectory() 的Javadoc,如果它发现该目录已经存在,它将抛出一个FileAlreadyExistsException。尽管 Javadoc 在关于方法抛出的异常的部分中确实提到了FileAlreadyExistsException - if a directory could not otherwise be created because a file of that name already exists (optional specific exception),但它说的是“文件”而不是“目录”,直到我在这里看到这个答案之前我对此感到困惑。
【解决方案2】:

Files.createDirectory 实际上是创建目录 -> “创建一个新目录..... createDirectories 方法应该用在需要先创建所有不存在的父目录的地方。”

如果要确保文件存在,只需使用 file.exists() 方法

【讨论】:

  • 这正是我想做的,创建所有不存在的目录。
【解决方案3】:

Java 7 documentation 已经提到你会得到一个FileAlreadyExistsException。那么问题似乎是什么?

【讨论】:

  • 确实如此,没注意到。但是,当它从描述中推断出如果目录已经存在,它应该继续愉快地继续呢?
  • 不,没有推论。抛出异常仅仅意味着它不会继续。也许你不在乎,但其他人可能会。
  • 我误解了文档 - 我认为它不会在意。
  • @ChrisWatts Javadoc 实际上有点含糊;它说FileAlreadyExistsException - if a directory could not otherwise be created because a file of that name already exists (optional specific exception),但实际上,在两种情况下会引发异常:具有相同名称的文件(包括文件扩展名)或目录已存在。
【解决方案4】:
if (!Files.isDirectory(Paths.get(dirpath))) {
    System.out.println("Output Files parent path does not exist:"+dirpath);
    File file = new File(dirpath);
    if (!file.exists()) {
        if (file.mkdir()) {
            System.out.println("Output files directory is created!");
        } else {
            System.out.println("Failed to create output directory!");
        }
    }
}

先检查再创建!!

【讨论】:

    猜你喜欢
    • 2020-05-23
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2017-02-13
    • 1970-01-01
    相关资源
    最近更新 更多