package cn.edut.tarena.demo1;
import java.io.*;
import org.junit.Test;
public class Demo_copy {
@Test
public void test001() {
copyAll("D:\\a", "D:\\b\\c\\d\\e\\f");
}
public void copyAll(String pathIn, String pathOut) {
File fileIn = new File(pathIn) ;
File fileOut = new File(pathOut) ;
if(fileIn.isFile()) {
try {
copuFile(fileIn , fileOut) ;
} catch (IOException e) {
e.printStackTrace();
}
}else if(fileIn.isDirectory()){
fileOut.mkdirs();
File[] fs = fileIn.listFiles();
for(File f : fs) {
copyAll(f.getAbsolutePath(), pathOut+"\\"+f.getName());
}
}else {
throw new RuntimeException();
}
}
public void copuFile(File fileIn , File fileOut) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileIn)) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileOut));
int b ;
byte[] bs = new byte[1024*8] ;
while((b = bis.read(bs))!=-1) {
bos.write(bs);
}
bos.flush();
bis.close();
bos.close();
}
}