1.用递归方式显示文件夹------->效果图
代码实现
代码语言:javascript
复制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();
}
}
运行结果
代码语言:javascript
复制|e:aaa
|----e:aaaaa - 副本 (2).ppt
|----e:aaaaa - 副本.ppt
|----e:aaaaa.ppt
|----e:aaabbb
|--------e:aaabbbbbb.ppt
|--------e:aaabbbccc
|------------e:aaabbbcccdasd.ppt
|--------e:aaabbbhello
Process finished with exit code 0