你可以参考一下!
package com.itheima;
import java.io.*;
public class CopyFile {
/**
* @param args
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
copy("F:\\java练习","G:\\java练习");
System.out.println("文件已复制,请查看G:\\java练习");
}
public static void copy(String src,String des)throws IOException{
File file1 = new File(src);
File file2 = new File(des);
File[] files = file1.listFiles();
if(!file2.exists()){
file2.mkdirs();
}
for(File f : files){
if(f.isFile()){
filecopy(f.getPath(),des+"\\"+f.getName());
}else if(f.isDirectory()){
copy(f.getPath(),des+"\\"+f.getName());
}
}
}
public static void filecopy(String src,String des)throws IOException{
BufferedInputStream bufis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(des));
int len = 0;
while((len=bufis.read())!=-1){
bufos.write(len);
}
bufis.close();
bufos.close();
}
}
|