我想将文件复制到将由目录选择器选择的目录中。为此我已经尝试过..
DirectoryChooser di = new DirectoryChooser();
File src = new File("C:/Users/ir.emon/Desktop/dump1.png");
File dest = di.showDialog(null);
try {
Files.copy(src.toPath(), dest.toPath());
} catch (Exception e) {
e.printStackTrace();
}
但这总是给出“java.nio.file.FileAlreadyExistsException
”,尽管我想要复制的文件不存在于目录选择器选择的所需目录中。我还尝试添加 StandardCopyOption.REPLACE_EXISTING
选项。但在这种情况下,目标(目标)文件夹已损坏。有人可以帮我解决这个问题吗?提前致谢。
最佳答案
来自API docs :
target - the path to the target file (may be associated with a different provider to the source path)
您没有提供文件作为目标,而是提供了包含的目录。所以你的代码的行为是完全合理的。
按如下方式更改代码:
Files.copy(src.toPath(), new File(dest, "deservedFilename.ext").toPath());
关于java - 通过目录选择器复制选定目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46421414/