概述:本文实现把某一目录下的所有文件展开成目录树形式,效果如下:
程序如下:
public static void genDirTree(String path, int level, String dir) {
level++;
File file = new File(path);
File[] files = file.listFiles();
if (!file.exists()) {
System.out.println("文件不存在");
return;
}
if (files.length != 0) {
for (File f : files) {
if (f.isDirectory()) {
dir = f.getName();
System.out.println(levelSign(level) + dir);
genDirTree(f.getAbsolutePath(), level, dir);
} else {
System.out.println(levelSign(level) + f.getName());
}
}
}
}
//文件层级信息
private static String levelSign(int level) {
StringBuilder sb = new StringBuilder();
sb.append(" ├─");
for (int x = 0; x < level; x++) {
sb.insert(0, " │ ");
}
return sb.toString();
}