文件复制陷入无限循环

时间:2010-12-17 13:07:10

标签: java file-io recursion

以下代码似乎陷入了无限循环。我必须终止程序才能阻止它运行。

这是输出&代码 -

File copied from c:\projects\test\buildlist.txt to c:\projects\test\newtest\buildlist.txt
File copied from c:\projects\test\GHTELE5S605A.jad to c:\projects\test\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\GHTELE5S605A.jar to c:\projects\test\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\GHTele5T240w400h.jad to c:\projects\test\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\GHTele5T240w400h.jar to c:\projects\test\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\buildlist.txt to c:\projects\test\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\newtest\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\newtest\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\newtest\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\newtest\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\newtest\newtest\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\buildlist.txt
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\GHTELE5S605A.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jad
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\GHTele5T240w400h.jar
File copied from c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\buildlist.txt to c:\projects\test\newtest\newtest\newtest\newtest\newtest\newtest\newtest\newtest\buildlist.txt

-

public static void main(String args[]) throws IOException{

    args = new String[2];
    args[0] = "c:\\projects\\test";
    args[1] = "c:\\projects\\test\\newtest";

        File srcFolder = new File(args[0]);
        File destFolder = new File(args[1]);

        //make sure source exists
        if(!srcFolder.exists()){

           System.out.println("Directory does not exist.");
           //just exit
           System.exit(0);

        }else{

           try{
            copyFolder(srcFolder,destFolder);
           }catch(IOException e){
            e.printStackTrace();
            //error, just exit
                System.exit(0);
           }
        }

        System.out.println("Done");
    }

    public static void copyFolder(File src, File dest)
        throws IOException{

        if(src.isDirectory()){

            //if directory not exists, create it
            if(!dest.exists()){
               dest.mkdir();
               System.out.println("Directory copied from " 
                              + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

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

        }else{
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
                OutputStream out = new FileOutputStream(dest); 

                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 " + src + " to " + dest);
        }
    }
}

感谢您的帮助。

4 个答案:

答案 0 :(得分:4)

在循环列出的文件时过滤掉目标目录。

if (src.equals(destFolder)) {
    continue;
}

我建议您使用File[] files = src.listFiles();代替src.list()

请注意,将数组括号放在类型而不是变量上是更好的样式,即File[] files而不是File files[]

另请注意:您在阅读文件时遇到错误。即使尚未到达文件末尾,InputStream.read(byte[])也可能返回0,请检查!= -1而不是> 0

答案 1 :(得分:1)

try{
    copyFolder(srcFolder,destFolder);     //  <-- this is where your problem lies
}catch(IOException e){

你的程序进入无限递归,因为在这个方法中,你再次为新创建的目录调用它。

您应该重写它,以便它只递归遍历源目录。

答案 2 :(得分:1)

如果您不需要自己编写复制功能,则应使用库来执行此操作。与Apache commons IO一样。

否则,请检查您是否未将目的地(作为来源)复制到目的地。

答案 3 :(得分:0)

问题来自于您将文件复制到源目录的子目录。

列出要复制的文件和目录时,应该排除目标目录,否则你确实会陷入无限循环:

String files[] = src.list();
for (String file : files) {
    // Test if the file is equal to the destination directory;
    // - if so, just skip it ! (continue)
    // - if not so, you can process the file
}