带有 REPLACE_EXISTING 选项的 FileAlreadyExistsException
FileAlreadyExistsException with REPLACE_EXISTING option
在我的代码中有一个用另一个文件替换一个文件的循环。
这是通过以下方式完成的:
java.nio.file.Files.move(Path source, Path target, CopyOption... options) throws IOException
抛出以下异常:
Exception in thread "main" java.nio.file.FileAlreadyExistsException: C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat.temp -> C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
at java.nio.file.Files.move(Unknown Source)
(*) at bruteforce.Main.changeValue(Main.java:260)
at bruteforce.Main.main(Main.java:71)
抛出异常的行:
(*) at bruteforce.Main.changeValue(Main.java:260):
Files.move(path, path.resolveSibling("DESTINY_FILE"), REPLACE_EXISTING);
Javadoc 定义异常:
...
FileAlreadyExistsException - if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not specified (optional specific exception)
...
代码明确指定REPLACE_EXISTING.
另外在文件开头导入选项:
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
有什么提示吗?
这可能是由于计算机(或硬盘)休眠或类似的原因?
我设置了使用电网时不暂停的能源选项。
提前致谢
Files.move 不是原子操作(当然除非指定了 ATOMIC_MOVE),所以我假设是发生了一些其他 IO 锁定了这个文件。
请确保您:
Lunch close()
方法在此资源上或使用 try-with-resources
您的 OS 没有使用此文件(例如,在记事本中打开,您午餐
tail反对它)
如果您的文件经常被访问,那么您可以尝试创建循环来检查 Files.isWritable()
在我的代码中有一个用另一个文件替换一个文件的循环。
这是通过以下方式完成的:
java.nio.file.Files.move(Path source, Path target, CopyOption... options) throws IOException
抛出以下异常:
Exception in thread "main" java.nio.file.FileAlreadyExistsException: C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat.temp -> C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
at java.nio.file.Files.move(Unknown Source)
(*) at bruteforce.Main.changeValue(Main.java:260)
at bruteforce.Main.main(Main.java:71)
抛出异常的行:
(*) at bruteforce.Main.changeValue(Main.java:260):
Files.move(path, path.resolveSibling("DESTINY_FILE"), REPLACE_EXISTING);
Javadoc 定义异常:
...
FileAlreadyExistsException - if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not specified (optional specific exception)
...
代码明确指定REPLACE_EXISTING.
另外在文件开头导入选项:
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
有什么提示吗?
这可能是由于计算机(或硬盘)休眠或类似的原因? 我设置了使用电网时不暂停的能源选项。
提前致谢
Files.move 不是原子操作(当然除非指定了 ATOMIC_MOVE),所以我假设是发生了一些其他 IO 锁定了这个文件。
请确保您:
Lunch
close()
方法在此资源上或使用 try-with-resources您的 OS 没有使用此文件(例如,在记事本中打开,您午餐 tail反对它)
如果您的文件经常被访问,那么您可以尝试创建循环来检查 Files.isWritable()