package com.et.test;
import java.io.File;
/*
* 模拟
* MorceSoft Dos命令tree
* F:.
* ├─Downloads
* ├─Favorites
* │ └─Links
* │ ├─one.txt
* │ ├─two.txt
* │ └─three.txt
* ├─Links
* ├─Music
* ├─Pictures
* ├─Saved Games
* ├─Searches
* └─Videos
*/
public class FileDirectory {
public static void main(String[] args) {
File file = new File("D:");
//文件对象实际存在,并且是目录,递归方法清晰的显示包含的所有文件、目录
if(file.exists()) {
getDeptTree(file,"");
}
}
protected static void getDeptTree(File f, String prefix) {
if(f.listFiles() != null){
//获取目录下的所有文件和子目录
File[] childs = f.listFiles();
//遍历file数组
for (int i = 0; i < childs.length;i++) {
String strLine = "";
String strStart = "";
if (childs.length - i > 1) {
strStart = prefix + " │";
} else {
strStart = prefix + " ";
}
if (i == childs.length - 1) {
strLine = prefix + " └─";
} else {
strLine = prefix + " ├─";
}
//如果是文件,打印文件名及大小
if(childs[i].isFile()) {
System.out.println(strLine + childs[i].getName()+" "+childs[i].length()+"B");
}
//如果是目录,打印目录名,执行递归方法
if (childs[i].isDirectory()) {
System.out.println(strLine + childs[i].getName());
getDeptTree(childs[i], strStart);
}
}
}
}
}
Java 编程题 查开磁盘目录树结构
最新推荐文章于 2023-08-12 00:04:07 发布