import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class FileTest03 {
static String sourcePath = "填入你的代码地址";
public static void main(String[] args) {
File file = new File(sourcePath);
HashMap<String, String> map = new HashMap<>();
Set<Map.Entry<String, String>> entries = map.entrySet();
getFile(file.listFiles(), map);
}
public static void getFile(File[] files, HashMap<String, String> map) {
for (File f : files) {
if (f.isDirectory()) {
//空格
String whiteSpace = createWhiteSpace(f.getPath());
//目录
String name = f.getName();
System.out.println(whiteSpace + name);
getFile(f.listFiles(), map);
} else {
//输出空格和文件名
System.out.println(createWhiteSpace(f.getPath()) + f.getName());
map.put(f.getPath(), f.getName());
}
}
}
public static String createWhiteSpace(String tempPath) {
String str = "";
String[] split = sourcePath.split("\\\\");
String[] temp = tempPath.split("\\\\");
int whiteNum = temp.length - split.length;
for (int i = 0; i < whiteNum; i++) {
str += " ";
}
return str;
}
}
输出示例
版权声明:本文为fangjial原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。