Files.move 和 Files.copy 正在抛出 java.nio.file.FileAlreadyExistsException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37721668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Files.move and Files.copy is throwing java.nio.file.FileAlreadyExistsException
提问by aga
I want to delete one file and rename another file with the old file but I am not able to move this file as java is throwing java.nio.file.FileAlreadyExistsException
Following is the code snippet I am using
我想删除一个文件并用旧文件重命名另一个文件,但我无法移动这个文件,因为 java 正在抛出java.nio.file.FileAlreadyExistsException
以下是我正在使用的代码片段
static void swapData(String origFilePath, String tempFilePath) throws IOException{
Path tempPath = FileSystems.getDefault().getPath(tempFilePath);
Path origPath = FileSystems.getDefault().getPath(origFilePath);
try{
String origFileName = null;
File origFileRef = new File(origFilePath);
if(Files.exists(origPath)){
origFileName = origFileRef.getName();
Files.delete(origPath);
if(Files.exists(origPath))
throw new IOException("cannot able to delete original file");
}
if(origFileName != null)
Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
}catch(IOException e){
throw e;
}
}
Here is the exception I am recieving on
Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
这里是我recieving异常上
Files.move(tempPath, tempPath.resolveSibling(origFileName), StandardCopyOption.REPLACE_EXISTING);
Also when I see this file in windows explorer, its thumbnail is present but cannot able to open it. I am not able to understand why it is happening and If I am using REPLACE_EXISTING, why it is throwing FileAlreadyExistsException exception.
此外,当我在 Windows 资源管理器中看到此文件时,它的缩略图存在但无法打开它。我无法理解为什么会发生这种情况,如果我正在使用 REPLACE_EXISTING,为什么它会抛出 FileAlreadyExistsException 异常。
Also I edited the previous question as it is not clearly stated.
我也编辑了上一个问题,因为它没有明确说明。
Please help.
请帮忙。
Anuj
阿努吉
回答by dancnfoo
Check if you have another thread holding on to the same file resource while running Files.move
or Files.copy
. I had the same exception and file access symptom and was able to resolve it after serializing the file accesses.
检查在运行Files.move
或Files.copy
. 我有相同的异常和文件访问症状,并且能够在序列化文件访问后解决它。
Also, by using the REPLACE_EXISTING
option when doing Files.copy
or Files.move
, you no longer need to code the multiple steps of deleting the original file and then renaming the tmp, although Files.move
or Files.copy
are not guaranteed atomic. There is a ATOMIC_MOVE
option, however I don't like the implementation specific guarantee where IOException
could be thrown if a file exists already as described by the javadoc.
此外,通过REPLACE_EXISTING
在执行Files.copy
or时使用该选项Files.move
,您不再需要编写删除原始文件然后重命名 tmp 的多个步骤,尽管Files.move
orFiles.copy
不能保证原子性。有一个ATOMIC_MOVE
选项,但是我不喜欢实现特定的保证,IOException
如果文件已经如 javadoc 所描述的那样存在,则可能会抛出该保证。
ATOMIC_MOVE : The move is performed as an atomic file system operation and all other options are ignored. If the target file exists then it is implementation specific if the existing file is replaced or this method fails by throwing an IOException. If the move cannot be performed as an atomic file system operation then AtomicMoveNotSupportedException is thrown. This can arise, for example, when the target location is on a different FileStore and would require that the file be copied, or target location is associated with a different provider to this object.
ATOMIC_MOVE :移动作为原子文件系统操作执行,所有其他选项都将被忽略。如果目标文件存在,那么如果现有文件被替换或此方法因抛出 IOException 而失败,则它是特定于实现的。如果移动不能作为原子文件系统操作执行,则抛出 AtomicMoveNotSupportedException。例如,当目标位置在不同的 FileStore 上并且需要复制文件时,或者目标位置与该对象的不同提供者相关联时,就会出现这种情况。