package itheima;
import java.io.*;
/**
* 复制整个文件夹的内容
* @author Administrator
*
*/
public class T14 {
public static void main(String[] args) throws Exception {
File file = new File("E:\\java测试题");
copyFile(file,new File("D:"));
}
private static void copyFile(File file,File fs) throws Exception {
if(!file.exists())
throw new RuntimeException("文件或目录不存在");
File f = new File(fs +"\\" + file.getName());
f.mkdir();
File[] files = file.listFiles();
for(int i=0; i<files.length; i++) {
if(files[i].isFile()) {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(files[i])));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fs + "\\" + files[i].getName())));
String s = null;
while((s=br.readLine()) != null) {
bw.write(s);
bw.newLine();
bw.flush();
}
} else {
copyFile(files[i],f);
}
}
}
}
刚刚把代码调出来,还好实现了 |