简体   繁体   English

如何将文件复制到其他目录

[英]How to copy files to other directory

I was listing the files in one folder and then I have to transfer them to another.我在一个文件夹中列出了文件,然后我必须将它们转移到另一个文件夹中。 However, the problem I get is the following ... When trying to paste them to the other folder the path is like E: \\ Files, which causes me to generate some kind of file and it does not stick to me like it should .但是,我遇到的问题如下......当试图将它们粘贴到另一个文件夹时,路径就像 E:\\ Files,这导致我生成某种文件,但它不会像它应该的那样粘在我身上。 I tried several ways and still can not do it, I leave my code to see if you can help me我试了好几种方法还是不行,我留下我的代码看看你能不能帮我

 Path algo = Paths.get("E:/Files/");
   public void Copy(String origenArchivo, Path algo) {
                Path origenPath = Paths.get(origenArchivo);
                String s = algo.toAbsolutePath().toString();
                System.out.println(s);
                Path destinoPath = Paths.get(s);
                System.out.println(destinoPath);
                String x = destinoPath.toString() + "/";

                Path conv = Paths.get(x);
                System.out.println(conv);
        try {

            Files.copy(origenPath, conv, StandardCopyOption.REPLACE_EXISTING);
               } catch (IOException ex) {
            Logger.getLogger(Metodos.class.getName()).log(Level.SEVERE, null, ex);
        }



    }




 File dir = new File("C:/Users/PC/Desktop/");
        public void TravelToFantasy(File dir) {
        File listFile[] = dir.listFiles();
        if (listFile != null) {
            for (int i = 0; i < listFile.length; i++) {
                if (listFile[i].isDirectory()) {
                    Copy(listFile[i]);
                } else {
                    System.out.println(listFile[i].getPath());
                    System.out.println(destino);
                    this.Copy(listFile[i].getPath(), algo);

                }
            }
        }
         }

I was trying to put the "/" to the path Paths.get gets me, but it always ends up leaving me the path as E:\\Files我试图将“/”放在路径 Paths.get 上,但它总是以 E:\\Files 结束我的路径

Thanks four you!谢谢四位!

You cannot pass a directory to Files.copy.您不能将目录传递给 Files.copy。

You don't need all that conversion to and from strings.您不需要所有与字符串之间的转换。 Just use Path.resolve instead:只需使用Path.resolve代替:

public void copy(String origenArchivo, Path algo)
throws IOException {
    Path origenPath = Paths.get(origenArchivo);
    Path conv = algo.resolve(origenPath.getFileName());
    Files.copy(origenPath, conv, StandardCopyOption.REPLACE_EXISTING);
}

As a side note, the Path/Paths/Files classes are superior to the java.io.File class, because they provide meaningful information if an operation fails.作为旁注,Path/Paths/Files 类优于 java.io.File 类,因为它们在操作失败时提供有意义的信息。 You should not use java.io.File at all:你根本不应该使用 java.io.File :

Path dir = Paths.get("C:/Users/PC/Desktop/");

public void TravelToFantasy(Path dir)
throws IOException {
    try (DirectoryStream<Path> listFile = Files.newDirectoryStream(dir)) {
        for (Path file : listFile) {
            if (Files.isDirectory(file)) {
                Copy(file);
            } else {
                System.out.println(file);
                System.out.println(destino);
                this.Copy(file, algo);
            }
        }
    }
}

使用 java.io.File.separator 而不是“/”,这将有助于您的代码运行任何操作系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM