Java Filename Filters
Implementing FilenameFilter in a Class
Here is an example class that filters out everything that is not a Java source code file.
import java.io.*;
public class javaFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
if (name.endsWith(".java"))
return true;
return false;
}
}
Files can be filtered using any criteria you like. An accept() method may test modification date, permissions, file size, and any attribute Java supports.
You cannot filter by attributes Java does not support, like Macintosh file and creator codes, at least not without native methods or some sort of access to the native API.
This accept()
method tests whether the file ends with .html and is in a directory where the program can read files:
public boolean accept(File directory, String name) {
if (name.endsWith(".html") && directory.canRead()) {
return true;
}
return false;
}