楼主 看下我的代码能不能复制文件夹
package foldercopy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FolderCopy {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//f1和f2是目录,目录自己改写下
File f1=new File("D:\\有道翻译");
File f2=new File("c:\\123");
copy(f1, f2);
}
public static void copy(File file1,File file2){
File file[]=file1.listFiles();
if(file2.exists()==false){
file2.mkdir();
}
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
copyFile(file[i],new File(file2,file[i].getName()));
}
else{
copy(file[i], new File(file2,file[i].getName()));
}
}
}
public static void copyFile(File scr,File des){
BufferedInputStream bufferedInputStream=null;
BufferedOutputStream bufferedOutputStream=null;
try {
bufferedInputStream=new BufferedInputStream(new FileInputStream(scr));
bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(des));
int j=0;
byte b[]=new byte[1024*1024];
while((j=bufferedInputStream.read(b))!=-1){
bufferedOutputStream.write(b, 0, j);
bufferedOutputStream.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
bufferedInputStream.close();
bufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
|