package lianxi;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
/**
* 3、编写程序,将指定目录下所有的.java文件拷贝到另一个目的中,将扩展名改为.txt。
* @author jjr
*
*/
public class Test3 {
public static void main(String[] args) {
File dir = new File("C:\\Users\\jjr\\Desktop\\aaa");
File target = new File("D:\\develop");
try {
copyFile(dir,target);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(File dir,File target) throws Exception {
(new File(target,dir.getName())).mkdir();
File tf = new File(target,dir.getName());
File[] names = dir.listFiles();
for (File file : names) {
if (file.isDirectory()) {
copyFile(file,tf);
}
if (file.isFile() && file.getName().endsWith(".java")) {
BufferedReader bufr = new BufferedReader(new FileReader(file));
File txt = new File(tf,file.getName().split("\\.")[0].concat(".txt"));
BufferedWriter bufw = new BufferedWriter(
new FileWriter(txt));
String s = null;
while ((s=bufr.readLine())!=null) {
bufw.write(s);
bufw.newLine();
bufw.flush();
}
bufw.close();
bufr.close();
}
}
}
}
|
|