import java.io.File;
public class PrintDirs {
public static void main(String[] args) {
showDir(1,new File("/home/huanyu/Documents"));
}
public static void showDir(int indent, File file){
/**
* 打印一级目录名称
*/
for (int i=0;i<indent;i++)
System.out.print("-");
System.out.println(file.getName());
/**
* 判断一级目录是否是目录,是则打印子目录
*/
if (file.isDirectory()){
File[] files = file.listFiles();
for (int i=0; i<files.length;i++){
showDir(indent+4,files[i]);
}
}
}
}
![img_c1a3b40c7391e4ecae8908c9d0f22cf3.png](https://yqfile.alicdn.com/img_c1a3b40c7391e4ecae8908c9d0f22cf3.png?x-oss-process=image/resize,w_1400/format,webp)