文件复制陷入无限循环

发布于 2024-10-08 06:31:21 字数 7538 浏览 3 评论 0原文

下面的代码似乎陷入了无限循环。我必须终止该程序才能停止其运行。

这是输出&代码 -

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

谢谢你的帮助。

The below code seems to be stuck in an infinate loop. I have to terminate the program to stop it running.

Here's the output & the code -

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

Thank you for an help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慕巷 2024-10-15 06:31:21

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

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

我建议您使用 File[] files = src.listFiles(); 而不是 src.list()

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

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

Filter out the destination directory when looping over the listed files.

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

I recommend you use File[] files = src.listFiles(); instead of src.list().

And note that it is better style to put the array brackets on the type instead of the variable, that is File[] files instead of File files[].

Also note: You have a bug when reading files. InputStream.read(byte[]) may return 0 even if end of file hasn't been reached, check for != -1 instead of > 0.

你是暖光i 2024-10-15 06:31:21
try{
    copyFolder(srcFolder,destFolder);     //  <-- this is where your problem lies
}catch(IOException e){

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

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

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

Your program goes into infinite recursion because inside this method, you're calling it once again for newly created directories.

You should rewrite it so it only recursively traverses the source directory.

瑾兮 2024-10-15 06:31:21

如果不需要您自己编写复制函数,则应该使用库来完成此操作。就像 Apache commons IO 一样。

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

If you are not required to write the copy function yourself, you should use a library to do that. Like the Apache commons IO.

Otherwise check if you are not copying the destination (as source) to the destination.

够运 2024-10-15 06:31:21

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

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

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
}

The problem comes from the fact that you are copying files to a sub-directory of the source directory.

When listing the files and directories to copy, you should exclude the target directory, or else you'll indeed get stuck in an infinite loop :

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文