package cn.itcast_03;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class demo2 {
public static void main(String[] args) throws IOException {
File srcFile = new File("F:\\BaiduYunDownload\\传智播客_2015年Java基础视频-深入浅出精华版\\day18\\code");
File destFile = new File("C:\\");
copyFolder(srcFile,destFile);
}
private static void copyFolder(File srcFile, File destFile) throws IOException {
if(srcFile.isDirectory()){
File newFolder = new File(destFile,srcFile.getName());
newFolder.mkdir();
File[] fileArray = srcFile.listFiles();
for(File file:fileArray){
copyFolder(file,srcFile);
}
}else{
File newFile = new File(destFile,srcFile.getName());
copyFile(srcFile,newFile);
}
}
private static void copyFile(File srcFile, File newFile) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
byte[] by = new byte[1024];
int len = 0;
while((len = bis.read(by))!=-1){
bos.write(by, 0, len);
}
bis.close();
bos.close();
}
}
|
|