我这有个垃圾的列子 你看下:package itheima.com.filer;
import java.io.*;
public class CopyFile {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String path="D:\\apache-tomcat-7.0.33";
File file = new File(path);
searchFile(file);
}
//递归并复制文件
private static void searchFile(File file) throws IOException {
File[] files = file.listFiles();
for (File sFile : files) {
if (sFile.isDirectory()) {
//递归
searchFile(sFile);
} else {
//F盘下文件目录
String newName = "F" + sFile.getPath().substring(1);
String dir=newName.substring(0, newName.lastIndexOf("\\"));
String FileName =sFile.getName();
File pathFile =new File(dir);
pathFile.mkdirs();
//F盘下目录和文件名
File newFile =new File (pathFile,FileName);
//复制文件
copyFileAll(sFile, newFile);
}
}
}
//复制文件
private static void copyFileAll(File oldFile, File newFile)
throws IOException {
BufferedReader br= new BufferedReader(new InputStreamReader(new FileInputStream(oldFile)));
BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile)));
String line=null;
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
这个代码不是很好 希望能帮到你
|