gpt4 book ai didi

java - Files.createDirectories() 抛出 FileAlreadyExistsExceptions 但没有目录

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:01 26 4
gpt4 key购买 nike

This question问了类似的问题。但是,就我而言,在调用 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
}

这里是异常(exception)...

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 block 中。

+--------------------------------------+--------+----------+--------------------------+---------+-----------+-------+--------+---------+-------+-------+
| 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() 之前检查目录是否存在,并在调用后验证该目录不存在。这种异常(exception)情况很少发生。我不明白什么?如何创建目录?如果我只是重复调用 Files.createDirectories(),它会继续失败。

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

最佳答案

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

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

关于java - Files.createDirectories() 抛出 FileAlreadyExistsExceptions 但没有目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53146301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com