package cn.java.io.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Demo09 {
public static void dirsCopy(File srcPath,File destPath){
File copyPath = destPath;
if(srcPath.isDirectory()){
copyPath = new File(destPath,srcPath.getName());
}
copyPath.mkdirs();
try {
fileDatilCopy(srcPath,copyPath);
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读写失败");
e.printStackTrace();
}
}
private static void fileDatilCopy(File srcPath,File copyPath) throws FileNotFoundException, IOException {
if(srcPath.isFile()){
try {
fileCopy(srcPath,copyPath);
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("文件读写失败");
e.printStackTrace();
}
}else if(srcPath.isDirectory()){
copyPath.mkdirs();
for(File sub:srcPath.listFiles()){
fileDatilCopy(sub,new File(copyPath,sub.getName()));
}
}
}
public static void fileCopy(File srcPath,File srcCopy) throws FileNotFoundException,IOException {
InputStream is = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(srcCopy,true);
byte[] by = new byte[100000];
int len = 0;
while(is.read(by)!= -1){
out.write(by,0,len);
}
out.flush();
out.close();
is.close();
}
public static void main(String[] args) {
File srcPath = new File("D:/myCode/temp");
File destPath = new File("D:/myCode/Temp_1");
dirsCopy(srcPath,destPath);
}
}