简体   繁体   English

将多个文件从一个文件夹复制到另一个文件夹 JAVA 15

[英]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.

相关问题 在Java中将文件从一个目录复制到另一个目录 - Copying files from one directory to another in Java 在Java中将文件从一个目录复制到另一个目录会引发NoSuchFileException - Copying files from one directory to another in java throws NoSuchFileException 将 .docx 文件从一个目录复制到另一个目录 - Copying .docx Files From One Directory to Another 如何使用Java将文件从一个文件夹复制到另一个文件夹? - How to copy files from one folder to another using Java? 需要使用 java 将所有文件从一个文件夹复制到另一个文件夹 - Need to copy all files from one folder to another using java 使用 Java Eclipse 将文件从一个文件夹复制到另一个文件夹(使用错误处理程序) - Copying a file from one folder to another (with error handler) using Java Eclipse 将属性从一个bean复制到另一个bean:Java - Copying Properties from one bean into another: Java 以独立于操作系统的方式将文件从一个文件夹复制到另一个文件夹 - Copying file from one folder to another in an OS independent way 将/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64中存在的OpenJDK文件夹从一个系统复制到另一个系统 - Copying OpenJDK folder present at /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.38.x86_64 from one system to another Java将一个文件复制到另一个文件 - Java copying one file into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM