{:2_32:}
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 CopyFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// 源目录
String y = "d:\\java";
File yuan = new File(y);
// 目的目录
String m = "e:\\java";
File mudi = new File(m);
//调用list方法遍历文件夹
list(yuan,mudi);
}
public static void list(File yfile, File mfiles) throws IOException {
// 建立目的文件夹
mfiles.mkdir();
// 创建文件数组
File[] files = yfile.listFiles();
for (File yuan : files) {
//判断是不是文件夹
if (yuan.isDirectory()) {
//获取文件夹名字
String name = yuan.getName();
//创建目的文件夹对象
File file = new File(mfiles, name);
//递归
list(yuan,file);
} else {
//获取源文件名字
String yfilename = yuan.getName();
//创建目的源文件对象
File file = new File(mfiles, yfilename);
//调用copyFiles方法
copyFiles(yuan, file);
}
}
}
public static void copyFiles(File yfile, File mfile) throws IOException {
// 高效字节输入流
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
yfile));
// 高效字节输出流
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(mfile));
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
bos.close();
bis.close();
}
} |
|