从键盘输入接收一个文件夹路径, 将该文件夹下的所有.java文件的文件名写入到java.txt文件中
看了N次了,还是没有看出来哪里错了,高手救急啊
public class Lesson {
public static void main(String[] args) throws IOException {
System.out.println("请输入一个路径:");
File dir = getDir();
String[] arr = dir.list();
BufferedWriter bw = new BufferedWriter(new FileWriter("java.txt"));
for (String name : arr)
if (name.endsWith(".java")) {
bw.write(name);
bw.newLine();
}
bw.close();
}
public static File getDir() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (true) {
File file = new File(br.readLine());
if (!file.exists())
System.out.print("您输入的路径不存在,请重新输入");
else if (!file.isDirectory())
System.out.println("您输入的不是文件夹路径,请重新输入");
else
return file;
}
}
}
|