【问题标题】:Why doesnt this copy class, copy the inner folders为什么不复制这个类,复制内部文件夹
【发布时间】:2015-02-15 05:24:58
【问题描述】:

我创建了一个复制类,它包含源文件夹和目标文件夹以及一组文件名。因此,此类搜索源文件夹,如果遇到与数组元素同名的文件,则将该文件复制到与源文件夹相同的文件夹结构中。 这是课程:

public class Copy {
    File src, dest; 
    ArrayList array;

    public Copy(File source, File destination ,ArrayList array) throws IOException{
        this.src = source;
        this.dest = destination;
        this.array = array;

        if(source.isDirectory()){

            //list all the directory contents
            String files[] = source.list();
            for (String element : files){ //Serch in all the files and if it match with a selected format, copies it directory
                if(array.contains(element)){
                    destination.mkdir();
                }
            };

            for (String file : files) {
            //construct the src and dest file structure
            File srcFile = new File(source, file);
            File destFile = new File(destination, file);
            //recursive copy

                new Copy(source,destination, array);
            }
        }
        else{
            //dest.mkdir();
            if(array.contains(source.getName())){
                //if file, then copy it
                //Use bytes stream to support all file types

                InputStream in = new FileInputStream(source);
                OutputStream out = new FileOutputStream(destination); 

                byte[] buffer = new byte[1024];

                int length;
                //copy the file content in bytes 
                while ((length = in.read(buffer)) > 0){
                    out.write(buffer, 0, length);
                }

                in.close();
                out.close();
                System.out.println("File copied from " + source + " to " + destination);
            }
        }
    }
}

这个类的问题是它只复制位于第一个或第二个内部文件夹中的文件。 例如,它可以成功复制这样的结构:

- Main Folder
    -Inner Folder1
      -File.pdf
    -Inner Folder2

但它不能复制这样的结构:

- Main Folder
    -Inner Folder1
        -Inner Inner Folder1
            -File.pdf
    -Inner Folder2

因此,如果文件位于多个内部文件夹中,则会出现错误:

线程“AWT-EventQueue-0”中的异常 java.lang.StackOverflowError

指向这一行:`new CopyFiles(src,dest, array);

有解决办法吗?

【问题讨论】:

  • 尝试将你的逻辑移到构造函数之外。
  • @ElliottFrisch 您的意思是将其转换为方法而不是类吗?
  • 我的意思是递归地创建对象实例只会为垃圾收集器工作,它会使调试更加困难;在您的情况下,一个(或两个)静态方法似乎更有意义(至少对我而言)。

标签: java file recursion


【解决方案1】:

我很想使用 Stings:

public Copy(String source, String destination ,ArrayList array) throws IOException{
    this.src = new File(source);
    this.dest = new File(destination);

在循环中你会遍历每个文件,但你使用相同的源和目标

       for (String file : files) {
        //construct the src and dest file structure
        File srcFile = new File(source, file);
        File destFile = new File(destination, file);
            new Copy(source,destination, array);
        }

【讨论】:

  • 感谢您的帖子,将 File 更改为 scring 会带来更多错误,因为 src 和 dest 将是字符串,并且诸如 src.isDirectory() 之类的方法对它们不起作用。如果我正确理解了你的答案。
  • this.src = new File(source);仍然会保持 src.isDirectory() 工作。但是转换为字符串可能需要比我在 sn-p 中写的更多的更改。这只是偏好。您的问题可能在始终发送相同源和目标的循环内。
  • 可能会添加更多 System.out.println();要获得更详细的信息,循环中发生了什么,并在错误行之前添加一个,这样您就可以看到崩溃前发生了什么。有些对象有很好的 toString() 方法,所以你可以从它们那里获得人类可读的信息。
  • 您的问题可能是您的过滤条件,当子文件夹中没有文件时,无论子文件夹中是否有任何匹配的文件,它都不会创建目录
【解决方案2】:

您似乎将错误的值传递给递归构造函数调用。尝试使用您创建的临时变量,使调用变为new Copy(srcFile, destFile, Arrays.asList(files));,并将您对ArrayList 的用法替换为List

编辑:

您的 for 循环也存在问题,它试图创建错误的目标目录。一起删除该循环,并将 make 目录逻辑添加到以下循环中。完成所有这些更改后,代码的 if 部分应如下所示:

//list all the directory contents
String files[] = source.list();
for (String file : files) {
    //construct the src and dest file structure
    File srcFile = new File(source, file);
    File destFile = new File(destination, file);
    //make new directory if needed
    if (srcFile.isDirectory()) {
        destFile.mkdir();
    }
    //recursive copy
    new Copy(srcFile, destFile, Arrays.asList(files));
}

这对我来说适用于您的示例目录结构,但初始目标必须存在。我用以下代码进行了测试:

ArrayList<String> array = new ArrayList<String>();
array.add("Inner Folder1");
new Copy(new File("C:\\Main Folder"), new File("C:\\New Folder"), array);

应该注意的是,Java 中已经有内置的方法可以做到这一点。除非您将此作为练习,否则您可能需要查看 Java API 中的 Files 类。

【讨论】:

  • 同样的结果。谢谢你的帖子。
  • 嗯,好主意,但构造函数只接受 ArrayLists 但文件是一个数组。在这种情况下,如何将String files[] = src.list(); 更改为数组列表
  • @Dan 我又更新了。我的错误,我应该更仔细地查看类型:)
  • @Dan ArrayList 扩展了List,因此任何调用此构造函数的代码仍然可以调用此构造函数,并传递ArrayList 而无需任何更改。 Arrays.asList 方法也会为您完成转换。
  • @Dan 希望我需要做的最后一次编辑;)我终于自己测试了代码(就像我应该从一开始就做的那样),看起来调用的循环有问题destination.mkdir();
猜你喜欢
  • 2014-04-10
  • 2018-01-05
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多