https://kyrenesjtv.github.io/
/**
* 深度查找文件名
* @param filePath
* @return 文件名list
*/
public static List<String> dfsDir(String filePath){
List<String> fileNmaeList = new ArrayList<>();
Stack<File> fileStack = new Stack<>();
File file = new File(filePath);
fileStack.push(file);
while(!fileStack.isEmpty()){
File currentFile = fileStack.pop();
//添加到name list
fileNmaeList.add(currentFile.getName());
//判断是否是文件夹
if(currentFile.isDirectory()){
for(File chileFile : currentFile.listFiles()){
fileStack.push(chileFile);
}
}
}
return fileNmaeList;
}
/**
* 广度查找文件名
* @param filePath
* @return 文件名list
*/
public static List<String> bfsDir(String filePath){
List<String> fileNmaeList = new ArrayList<>();
Queue<File> fileStack = new LinkedList<>();
File file = new File(filePath);
fileStack.offer(file);
while(!fileStack.isEmpty()){
File currentFile = fileStack.poll();
//添加到name list
fileNmaeList.add(currentFile.getName());
//判断是否是文件夹
if(currentFile.isDirectory()){
for(File chileFile : currentFile.listFiles()){
fileStack.offer(chileFile);
}
}
}
return fileNmaeList;
}