写一个主方法去调用一个子方法,用子方法加以判断循环调用自身即可。
public void getFile(String path){
if(ObjectUtils.isEmpty(path)){
throw new ServiceException("请输入文件路径!");
}
File file = new File(path);
if (!file.exists()) {
throw new ServiceException("文件路径不存在!");
}
if (!file.isDirectory()) {
throw new ServiceException("文件路径不是文件夹!");
}
List<File> fileList = new ArrayList<>();
getAllFileLists(file, fileList);
}
}
private void getAllFileLists(File file,List<File> fileList) {
File[] files = file.listFiles();
for(File f : files) {
if(f.isDirectory()){
getAllFileLists(f,fileList);
}else{
fileList.add(f);
}
}
}