public class FileExtensionFileFilter implements FileFilter {
private String extension;
public FileExtensionFileFilter(String extension) {
this.extension = extension;
}
/*
* Pass the File if it has the extension.
*/
public boolean accept(File file) {
// Lowercase the filename for easier comparison
String lCaseFilename = file.getName().toLowerCase();
return (file.isFile() && (lCaseFilename.indexOf(extension) > 0)) ? true
: false;
}
}
FileExtensionFileFilter 被用来屏蔽名称中不含字符串extension 的文件。它还屏蔽了子目录--这些子目录原本会让 listFiles() 方法正常返回。过滤器提供了一种很便利的方式选择性的向你的 Quartz 作业提供它能接受的作为输入的文件。
File dir = new File("/home/han/MyFile");
if (!dir.exists()) {
}
FileFilter filter = new FileExtensionFileFilter(".xls");
File[] files = dir.listFiles(filter);
if (files == null || files.length <= 0) {
return;
}
int size = files.length;
for (int i = 0; i < size; i++) {
File file = files[i];
File aFile = file.getAbsoluteFile();
long fileSize = file.length();
String msg = aFile + " - Size: " + fileSize;
}