首页 > 解决方案 > Files.createDirectories() 抛出 FileAlreadyExistsExceptions 但没有目录

问题描述

这个问题问了一个类似的问题。但是,在我的情况下,该目录在调用之前或之后不存在Files.createDirectories()。这发生在 Oracle JDK 10.0.2 上。

这是我的代码...

Set<PosixFilePermission> perms;
FileAttribute<?> attr;
Path path;
File directory;

directory = new File("/test/http/localhost_4452/UCF2b/Live");
path      = directory.toPath();
perms     = EnumSet.noneOf(PosixFilePermission.class);

perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);

attr = PosixFilePermissions.asFileAttribute(perms);

try
{
   if (!directory.exists())
      Files.createDirectories(path, attr);
}
catch (IOException e)
{
   if (!directory.exists())
   {
      ... collect more information about the state of the directory and its parent path ...
      ... add this information as a suppressed exception ...
      throw e;
   }
   // else do nothing and assume another thread created the directory
}

这是例外...

java.nio.file.FileAlreadyExistsException: /test/http/localhost_4452/UCF2b/Live
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:94)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:116)
at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:385)
at java.base/java.nio.file.Files.createDirectory(Files.java:682)
at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:789)
at java.base/java.nio.file.Files.createDirectories(Files.java:735)
at ...my code...

当前用户是root。这是收集的有关目录及其父目录的诊断信息。catch如果目录不存在,则在块中收集此信息。

+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+
|            Path                      | Exists |  Length  |        Modified          |  Owner  | Directory | File  | Hidden | Execute | Read  | Write |
+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+
| /test/http/localhost_4452/UCF2b/Live | false  |   0.00 B | 1970-01-01T00:00:00Z     | (null)  | false     | false | false  | false   | false | false |
| /test/http/localhost_4452/UCF2b      | true   |  4.00 kB | 2018-11-04T20:32:09.769Z | root    | true      | false | false  | true    | true  | true  |
| /test/http/localhost_4452            | true   |  4.00 kB | 2018-11-04T20:18:26.849Z | root    | true      | false | false  | true    | true  | true  |
| /test/http                           | true   |  4.00 kB | 2018-11-04T20:11:42.605Z | root    | true      | false | false  | true    | true  | true  |
| /test/                               | true   | 20.00 kB | 2018-11-04T20:32:09.768Z | root    | true      | false | false  | true    | true  | true  |
| /                                    | true   |  4.00 kB | 2018-11-04T20:09:22.061Z | root    | true      | false | false  | true    | true  | true  |
+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+

如您所见,代码在调用之前检查目录是否存在,并在调用Files.createDirectories()之后验证目录不存在。例外情况很少发生。我不明白什么?如何创建目录?如果我只是重复调用Files.createDirectories(),它会继续失败。

编辑:此代码由多个线程调用。这意味着多个线程可以调用Files.createDirectories(),但如果目录最终存在,代码不会重新引发异常。换句话说,其他线程必须在正确的时刻创建和删除目录,因为directory.exists()falsebefore 和 after Files.createDirectories()。此外,这个完美的时机必须坚持下去,因为一旦程序遇到这个问题,它就会在特定目录中不断发生。

标签: javaexceptioniodirectorysubdirectory

解决方案


这段代码很可能被多个线程使用,因为只有这样才能解释为什么if如果条件是代码会进入一个块false,而且它很少发生。一个线程检查该目录不存在,在它可以创建它之前,另一个线程创建它。如果是这种情况,您应该使用同步。

synchronized (this) {
    if (!directory.exists())
        Files.createDirectories(path, attr);
}

推荐阅读