importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.io.Reader;importjava.io.Writer;//练习:关于目录的拷贝?
public classText6 {//缓冲区大小
private static final int buffer_size = 1024*2;//源和目标检测
private static void doCopy(File src, File dst)throwsIOException {if(src.isFile()) {
copyFile(src, dst);
}else{
File dir=copyDirectory(src, dst);
File[] files=src.listFiles();if (files.length == 0) {
}else{for(File file : files) {
doCopy(file, dir);
}
}
}
}//拷贝实现
public static void copy(File src, File dst) throwsIOException {
doCopy(src, dst);
}//文件拷贝
private static void copyFile(File src, File dst) throwsIOException {
File file= newFile(dst, src.getName());
copy(new BufferedInputStream(new FileInputStream(src)), new BufferedOutputStream(newFileOutputStream(file)));
}//目录拷贝 要在目标设备中创建目录
private staticFile copyDirectory(File src, File dst) {
File file= newFile(dst, src.getName());
file.mkdir();returnfile;
}//拷贝
private static int copy(BufferedInputStream in, BufferedOutputStream out) throwsIOException {int byteCount = 0; //问号 byteCount
try{int bytesReader = 0;byte [] buffer = new byte[buffer_size];while ((bytesReader = in.read(buffer)) != -1) {
out.write(buffer,0, bytesReader);
byteCount+=bytesReader;
}
out.flush();
}finally{
in.close();
out.close();
}returnbyteCount;
}public static void main(String[] args)throwsIOException
{
File a=new File("F:\\测试的文件");
File b=new File("F:\\我的");
copy(a,b);
System.out.println("finish.................");
}
}