public static void main(String[] args) {
listFolder(folder(), 1);
}
public static File folder() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("please input a folder path:");
File file = new File(sc.nextLine());
if (!file.exists()) {
System.out.println("This file path don't exist");
} else if (file.isFile()) {
System.out.println("It's a file path;Input a folder path,please");
} else {
return file;
}
}
}
public static void listFolder(File file, int k) {
File[] f = file.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
File file1 = new File(dir, name);
return file1.isDirectory() || (file1.isFile() && file1.getName().endsWith(".java"));
}
});
for (File file2 : f) {
for (int i = 0; i < k; i++) {
System.out.print("\t");
}
System.out.println(file2);
if (file2.isDirectory())
listFolder(file2, k + 1);
}
}
}