package cn.itcast;
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 Test2 {
public static void main(String[] args) throws IOException {
//新旧文件夹路径
File srcDIR = new File("src/cn/itcast");
File destDIR = new File("HAHAHA");
//获取原文件夹下的所有文件对象
File[] file = srcDIR.listFiles();
//遍历数组,依次拿到每一个File对象
for (File oldFile : file) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(oldFile));
String fileName = oldFile.getName();
File newFile = new File(destDIR,fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));
//读
byte[] bytes = new byte[1024];
int len;
while((len=bis.read(bytes))!=-1){
//写
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
}
}
}
这是复制单级文件的,请问一下如何完成复制多级目录下的文件呢,研究了半天就是无从下手 |
|