电脑技术学习

利用java本地拷贝文件及文件夹

dn001
内容: 如何实现文件夹对文件夹的拷贝呢?如果文件夹里还有文件夹怎么办呢?

接下来我就将我的方法演示给一些初学者,希望能够帮助你们!

/*
* CopyRight by http://www.designac.org
*/
package copyfile;
import java.io.*;
/**
* @author Administrator
*
* Class Function:
*/
public class copyDirectory {
public static void main(String args[]) throws IOException {
String url1="C:/Downloads/kele";
String url2="d:/java/copyfile/copy";
(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(url2+"/"+file[i].getName(),url1+"/"+file[i].getName());
}
}
}
public static void copyDirectiory(String file1,String file2) throws IOException{
(new File(file1)).mkdirs();
File[] file=(new File(file2)).listFiles();
for(int i=0;i if(file[i].isFile()){
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(file1+"/"+file[i].getName());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
if(file[i].isDirectory()){
copyDirectiory(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}

}

}
}
Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd

标签: