import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
public class FilterFileTest implements FilenameFilter{
String suffix;
public FilterFileTest(String suffix) {
super();
this.suffix = suffix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
public List<File> getFileList(FilterFileTest filter,File dir,List<File> listFile) {
File[] files = dir.listFiles();
for(File file : files) {
if(file.isDirectory()) {
getFileList(filter,file,listFile);
} else {
if(accept(file.getParentFile(),file.getName())) {
listFile.add(file);
}
}
}
return listFile;
}
public boolean accept(File dir, String filePath) {
boolean bool = false;
File file = new File(filePath);
if(file.getName().endsWith(this.suffix)) {
bool = true;
}
return bool;
}
public static void main(String[] args) {
File dir = new File("G:\\test");
String suffix = ".pdf";
ArrayList<File> fileList = new ArrayList<File>();
FilterFileTest test = new FilterFileTest(suffix);
test.getFileList(test, dir, fileList);
for(File file : fileList) {
System.out.println(file.getPath());
}
}
}
**注意:FilenameFilter的accept方法通常在调用file.listFiles(FilenameFilter filter)或file.list(FilenameFilter filter)时
由系统调用**