1.用递归方式显示文件夹------->效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/42762c2191ac7263895e20ce08a7c832.png)
代码实现
package TestFile;/*
* zt
* 2020/8/6
* 17:46
*递归显示文件夹
*/
import java.io.File;
public class Demo1 {
public static void main(String[] args) {
listDir(new File("e:\\aaa"), 0);
}
public static void listDir(File dir,int level){
System.out.println(getSeprator(level)+dir.getAbsolutePath());
level++;
File[] files = dir.listFiles();
if(files != null && files.length > 0){
for (File file : files) {
if(file.isDirectory()){
listDir(file,level);
}else{
System.out.println(getSeprator(level)+file.getAbsolutePath());
}
}
}
}
//空格
public static String getSeprator(int level){
StringBuilder sb = new StringBuilder("|");
for (int i = 0; i < level; i++) {
sb.append("----");
}
return sb.toString();
}
}
运行结果
|e:\aaa
|----e:\aaa\aa - 副本 (2).ppt
|----e:\aaa\aa - 副本.ppt
|----e:\aaa\aa.ppt
|----e:\aaa\bbb
|--------e:\aaa\bbb\bbb.ppt
|--------e:\aaa\bbb\ccc
|------------e:\aaa\bbb\ccc\dasd.ppt
|--------e:\aaa\bbb\hello
Process finished with exit code 0