文件复制陷入无限循环
下面的代码似乎陷入了无限循环。我必须终止该程序才能停止其运行。
这是输出&代码 -
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
循环遍历列出的文件时过滤掉目标目录。
我建议您使用
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.
I recommend you use
File[] files = src.listFiles();
instead ofsrc.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 ofFile 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
.您的程序进入无限递归,因为在该方法中,您为新创建的目录再次调用它。
您应该重写它,以便它仅递归遍历源目录。
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.
如果不需要您自己编写复制函数,则应该使用库来完成此操作。就像 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.
问题来自于您将文件复制到源目录的子目录中。
当列出要复制的文件和目录时,您应该排除目标目录,否则您确实会陷入无限循环:
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 :