打印树状目录结构
程序员文章站
2022-06-12 13:44:10
...
[b][size=medium][color=darkblue]递归打印树状目录结构[/color][/size][/b]
package test;
import java.io.File;
public class Test {
public static void main(String[] args) {
File file = new File("F:/学习");
printFile(file, 1);
}
public static void printFile(File file, int level) {
for (int i = 1; i < level; i++) {
System.out.print("--");
}
System.out.println(file.getName());
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
printFile(file2, level + 1);
} else {
return;
}
}
}
}
上一篇: 聊聊广告系统里的匀速投放