import java.io.File;
public class ReFileExercise {
public static void main(String[] args) {
File file = new File("Documents");
FileTool.listAllFile(file);
}
}
class FileTool {
private static int deepth = 0;
public static void listAllFile(File file) {
String[] diectSplit = file.getPath().split("\\\\");
printBlank(deepth);
System.out.println(diectSplit[diectSplit.length - 1]+"\\");
File[] files = file.listFiles();
for (File f : files) {
deepth++;
if (f.isFile()) {
String[] fileSplit = f.getPath().split("\\\\");
printBlank(deepth);
System.out.println(fileSplit[fileSplit.length - 1]);
} else if (f.isDirectory()) {
listAllFile(f);
}
deepth--;
}
}
private static void printBlank(int num) {
for (int i = 0; i < num; i++) {
System.out.print(" ");
}
}
}
- 1
波猫747
import java.io.File;
public class FileExercise { static int deepth = 0;
}