为什么不复制这个副本类,复制内部文件夹

时间:2015-02-15 05:24:59

标签: java file recursion

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

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);

有什么想法解决它吗?

2 个答案:

答案 0 :(得分:1)

您似乎将错误的值传递给递归构造函数调用。尝试使用您创建的临时变量,以便调用变为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类。

答案 1 :(得分: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);
        }
相关问题