[英]Copying multiple files from one folder to another in JAVA 15
Path src = Paths.get("./resources");
Path dst = Paths.get("./trash");
try {
DirectoryStream<Path> ds = Files.newDirectoryStream(src);
for(Path fileorDir : ds) {
System.out.println(fileorDir);
Files.copy(fileorDir, dst);
}
}catch(IOException ioe){
ioe.printStackTrace();
}
//The error im getting is java.nio.file.FileAlreadyExistsException so from what i understand its trying to save the file to that exact location, not inside it, i need to save a couple text files this way, if i change the destination address to say trash/trash.txt it will save a file there called trash.txt. //我得到的错误是 java.nio.file.FileAlreadyExistsException 所以根据我的理解,它试图将文件保存到那个确切的位置,而不是里面,如果我改变目的地,我需要以这种方式保存几个文本文件地址说 trash/trash.txt 它将在那里保存一个名为 trash.txt 的文件。 but then on the next loop of the for each it throws a "Already exists" exception...
但是然后在 for each 的下一个循环中它抛出一个“已经存在”的异常......
Can somebody explain how i can just save all txt files into that folder from the src folder, as if dragging and dropping them?有人可以解释一下我如何将所有 txt 文件从 src 文件夹保存到该文件夹中,就像拖放它们一样吗?
Many thanks非常感谢
You can use a option in copy() who is StandardCopyOption.REPLACE_EXISTING but the problem is that dst isn't the good path.您可以在 copy() 中使用 StandardCopyOption.REPLACE_EXISTING 选项,但问题是 dst 不是好的路径。 For exemple, ressources/trash.txt should be copy in trash/trash.txt but dst is just /trash like path.
例如,ressources/trash.txt 应该复制到 trash/trash.txt 但 dst 只是 /trash 之类的路径。 Sorry for my english and it's my first answer:) Be merciful.
对不起我的英语,这是我的第一个答案:) 仁慈一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.