首页 > 解决方案 > Files.move 不断在目录上抛出文件已存在异常

问题描述

我正在尝试将文件从文件夹 A:“/files/attachments/2000/filename.txt”移动到文件夹 B:“/files/attachments/trashbin/2000/filename.txt”,但我不断收到异常:FileAlreadyExistsException异常输出是:“/files/attachments/trashbin/2000”。

这是代码:

Path filePath = Paths.get(pathOnNFS.concat(folderPath).concat(Utils.DELIMITER + fileName));
Path trashBinPath = Paths.get(trashBinNFS.concat(folderPath));
try {
    Files.createDirectories(trashBinPath);
    Files.move(filePath, trashBinPath.resolve(fileName), StandardCopyOption.REPLACE_EXISTING);
}catch(...){}

任何人都可以提出为什么会这样吗?这是文档:https ://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/nio/file/Files.html#move(java.nio.file.Path ,java.nio.file.Path,java.nio.file.CopyOption...)

标签: javafile-iofile-move

解决方案


推荐阅读