public static void main(String[] args) throws IOException {
//demo1();
demo2();
}
private static void demo2() throws IOException {
File f =getDir();
print(f);
}
private static void print(File f) throws IOException {
File fl = new File("F:\\TEST\\aaa");
File[] arr = f.listFiles();
for (File file : arr) {
if (file.getName().endsWith(".java")) {
int b;
FileReader r = new FileReader(file);
FileWriter w = new FileWriter(new File(fl, file.getName().replace(".java", ".txt")));
while ((b = r.read())!=-1) {
w.write(b);
}
r.close();
w.close();
}else if (file.isDirectory()) {
print(file);
}
}
}
private static File getDir() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个路径");
while (true) {
String line = sc.nextLine();
File f = new File(line);
if (!f.exists()) {
System.out.println("对不起,您输入的路径不存在");
}else if (f.isFile()) {
System.out.println("对不起,您输入的是一个文件路径");
}else {
return f;
}
}
} |
|