1.采用递归进行统计,确定需求不会有很多层的情况下使用,可以维护一个层数控制放入,控制遍历的次数(在不确定目录的深度的情况下)
/**
* 获取目录下所有的文件路径和对应文件的层数
*
* @param homePath
* @return
*/
public List<Map<String, Integer>> mapFiles(String homePath) {
List<Map<String, Integer>> savePathAndFile = new ArrayList<>();
File homeDir = new File(homePath);
if (homeDir.exists()) {
File[] files = homeDir.listFiles();
for (File file : files) {
if (file.exists()) {
if (file.isDirectory()) {
String containsChildDir = file.getAbsolutePath();
List<Map<String, Integer>> maps = mapFiles(containsChildDir);
maps = maps.stream().map(si -> {
for (String key : si.keySet()) {
Integer value = si.get(key);
si.put(key, ++value);//使用++value,维护文件的深度记录
}
return si;
}).collect(Collectors.toList());
savePathAndFile.addAll(maps);
} else {
Map<String, Integer> pathAndFile = new HashMap<>();
pathAndFile.put(file.getAbsolutePath(), 1);
savePathAndFile.add(pathAndFile);
}
}
}
}
return savePathAndFile;
}
增加一个deep版本
/**
* 获取目录下所有的文件路径和对应文件的层数,控制查找的层数
*
* @param homePath
* @return
*/
public static List<Map<String, Integer>> mapFiles(String homePath,Integer deep) {
List<Map<String, Integer>> savePathAndFile = new ArrayList<>();
File homeDir = new File(homePath);
if (homeDir.exists()) {
File[] files = homeDir.listFiles();
for (File file : files) {
if (file.exists()) {
if (file.isDirectory()) {
String containsChildDir = file.getAbsolutePath();
List<Map<String, Integer>> maps = mapFiles(containsChildDir,deep);
maps = maps.stream().map(si -> {
for (String key : si.keySet()) {
Integer value = si.get(key);
si.put(key, ++value);
}
return si;
}).collect(Collectors.toList());
if(maps.get(0).entrySet().iterator().next().getValue()>deep){
return savePathAndFile;
}
savePathAndFile.addAll(maps);
} else {
Map<String, Integer> pathAndFile = new HashMap<>();
pathAndFile.put(file.getAbsolutePath(), 1);
savePathAndFile.add(pathAndFile);
}
}
}
}
return savePathAndFile;
}
改造上述的代码,不采用递归
/**
* 获取目录下所有的文件路径和对应文件的层数,控制查找的层数,非递归
*
* @param homePath
* @return
*/
public static List<Map<String,Integer>> mapFiles(String homePath) {
List<Map<String,Integer>> savePathAndFile = new ArrayList<>();
LinkedList<String> queue= new LinkedList<>();
File homeDir = new File(homePath);
if(homeDir.exists()){
queue.add(homePath);
}
int deep=1;
while (!queue.isEmpty()){
String path = stack.pollLast();
File file = new File(path);
if(file.isDirectory()){
for (File f:file.listFiles()) {
queue.add(f.getAbsolutePath());
}
++deep;
}else{
Map<String, Integer> pathAndFile = new HashMap<>();
pathAndFile.put(file.getAbsolutePath(), deep);
savePathAndFile.add(pathAndFile);
}
}
return savePathAndFile;
}
增加层数控制
/**
* 获取目录下所有的文件路径和对应文件的层数,控制查找的层数,非递归
*
* @param homePath
* @return
*/
public static List<Map<String,Integer>> mapFiles(String homePath,int controlDeep) {
List<Map<String,Integer>> savePathAndFile = new ArrayList<>();
LinkedList<String> queue= new LinkedList<>();
File homeDir = new File(homePath);
if(homeDir.exists()){
queue.add(homePath);
}
int deep=1;
while (!queue.isEmpty()&&deep<=controlDeep){
String path = stack.pollLast();
File file = new File(path);
if(file.isDirectory()){
for (File f:file.listFiles()) {
queue.add(f.getAbsolutePath());
}
++deep;
}else{
Map<String, Integer> pathAndFile = new HashMap<>();
pathAndFile.put(file.getAbsolutePath(), deep);
savePathAndFile.add(pathAndFile);
}
}
return savePathAndFile;
}
放弃递归调用有效防止栈溢出,推荐采用此类操作。生产中采用递归有可能会引发事故,因此推荐非递归操作