如题,先贴源码。
package JiChu;
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 CopyFileDir2 {
public static void main(String[] args) throws Exception {
//System.err.println(new File("D:\\新建文件\\xin").toString());
copyFile(new File("D:\\新建文件"),new File("D:\\新建文件夹2"));
}
private static void copyFile(File filesrc, File filedest) throws IOException {
// TODO Auto-generated method stub
if(filesrc.isDirectory()){
filedest.mkdir();
File []temps = filesrc.listFiles();
for(File temp :temps){
if(temp.isDirectory()){
filedest =new File(filedest+File.separator+temp.getName());
System.out.println("目的文件是 "+filedest.toString()+" 源文件是 "+temp.toString());
copyFile(temp,filedest);
}
/*else{
copy(filedest, temp);
}*/
}
}
else
copy(filedest,filesrc);
}
private static void copy(File filedest, File temp)
throws FileNotFoundException, IOException {
/*if(!(filedest.exists()))
filedest.createNewFile();*/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temp));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filedest));
byte[] by = new byte[1024*50];
int len;
while((len=bis.read(by))!=-1){
bos.write(by, 0, len);
bos.flush();
}
bis.close();
bos.close();
}
}
|
|