获取指定路径下的所有指定类型的文件

/**
* 返回传入路径下的所有.conf文件
* @param filePath
* @return
* @throws FileNotFoundException
*/
public static ArrayList<File> getFilesEndWithConf(String filePath) throws FileNotFoundException {
ArrayList<File> files = new ArrayList<>();

File file = new File(filePath);
if (file.isDirectory()) {
File[] file_childrens = file.listFiles((dir,name)-> dir.isDirectory()||name.endsWith(".conf"));
for (File file_children:file_childrens){
if (file_children.isDirectory()){
files.addAll(FileUtils.getFilesEndWithConf(file_children.getPath()));
}else{
files.add(file_children);
}
}
return files;
} else if (file.isFile()){
files.add(file);
return files;
} else {
throw new FileNotFoundException("根据指定路径未找到文件:"+filePath);
}
}
posted @ 2020-06-09 17:48  BigWrite  阅读(445)  评论(0编辑  收藏  举报