java - 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()
是false
before 和 after Files.createDirectories()
。此外,这个完美的时机必须坚持下去,因为一旦程序遇到这个问题,它就会在特定目录中不断发生。
解决方案
这段代码很可能被多个线程使用,因为只有这样才能解释为什么if
如果条件是代码会进入一个块false
,而且它很少发生。一个线程检查该目录不存在,在它可以创建它之前,另一个线程创建它。如果是这种情况,您应该使用同步。
synchronized (this) {
if (!directory.exists())
Files.createDirectories(path, attr);
}
推荐阅读
- android - Renci.SshNet 与 Xamarin Android 的 SSH / SCP 命令失败
- php - 如何在自定义简码中获取 WooCommerce 产品对象以避免错误
- html - 如何应用文本溢出:省略号和溢出:未设置在一起
- android - 在android compose中找不到同时使用底部工作表和底部导航栏的方法
- android - 在 Kotlin 中显示对话框时如何隐藏片段中的底部导航栏?
- javascript - 将对象数组添加到另一个对象数组
- graphics - 具有多个着色器的 WebGL 渲染顺序/组织/流程
- ios - AudioKit - 尝试在 Audio Kit 5 中生成声音
- python - grequest超时后如何使用grequest自动重新连接
- javascript - VueJS:如何将数据从 v-for 列表传递给组件