package com.fangun;
import java.io.*;
public class CopyAll {
public static void main(String[] args) {
File src = new File("D:\\Downloads\\armadillo-9.900.3");
File des = new File("E:\\");
copy(src,des);
}
private static void copy(File src, File des) {
if (src.isFile()){
FileInputStream fileInputStream=null;
FileOutputStream fileOutputStream=null;
try {
fileInputStream = new FileInputStream(src);
String s = (des.getAbsolutePath().endsWith("\\") ? des.getAbsolutePath() : des.getAbsolutePath() + "\\") + src.getAbsolutePath().substring(3);
File file = new File(s);
if (!(file.exists())){
boolean newFile = file.createNewFile();
}
fileOutputStream = new FileOutputStream(s);
byte[] bytes = new byte[1024 * 1024];
int readCount=0;
while ((readCount=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes);
fileOutputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
File[] files = src.listFiles();
for (File file:files){
if (file.isDirectory()){
String s = (des.getAbsolutePath().endsWith("\\") ? des.getAbsolutePath() : des.getAbsolutePath() + "\\") + file.getAbsolutePath().substring(3);
File file1 = new File(s);
if (!(file1.exists())){
boolean mkdirs = file1.mkdirs();//创建父子目录
}
}
copy(file,des);
}
}
}
11-08
223
11-08
107
11-07
354