JAVA获取某个文件夹下的所有文件
需要JDK1.8 不支持@Data注解的可以删掉用IDE生成getter setter
package com.plugin;
import lombok.Data;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
@Data
public class FileFinder {
// 要查找的文件目录
private String path;
// 在文件目录下发现文件时的监听器(即一发现文件就会被这个类监听到)
private List<Consumer<File>> fileFoundListener;
// 文件过滤规则
private List<FileFilter> ignoreFilters;
private FileFinder(){
}
/**
* 创建一个文件查找器
*
* @param path 要查找的文件路径
* @return FileFinder
*/
@SuppressWarnings("all")
public static FileFinder getInstance(String path) throws FileNotFoundException {
if (path == null || path.trim().equals("")) {
throw new FileNotFoundException("path不能为空");
}
FileFinder fileFinder = new FileFinder();
fileFinder.setPath(path);
File file = new File(path);
if (!file.exists()) {
throw new FileNotFoundException("文件路径不存在:"+path);
}
if (!file.isDirectory()