Java FileFileFilter-class And Method Code Example
Here is an example of how to use the FileFileFilter
class from the org.apache.commons.io package in Java:
import java.io.File;
import org.apache.commons.io.filefilter.FileFileFilter;
public class FileFileFilterExample {
public static void main(String[] args) {
// Create a FileFileFilter that only accepts files (not directories)
FileFileFilter fileFileFilter = FileFileFilter.FILE;
// Use the filter to list all files (not directories) in the current directory
File dir = new File(".");
File[] files = dir.listFiles(fileFileFilter);
for (File file : files) {
System.out.println(file.getName());
}
}
}
In this example, we create an instance of FileFileFilter by calling the FileFileFilter.FILE static field.
Then we use the filter to list all files (not directories) in the current directory by calling the listFiles() method on a File object representing the current directory, and passing in the fileFileFilter object as an argument.
You can also use FileFilterUtils.fileFileFilter() to return a filter that only accepts files.
import java.io.File;
import org.apache.commons.io.filefilter.FileFilterUtils;
public class FileFileFilterExample {
public static void main(String[] args) {
// Create a FileFilterUtils that only accepts files
FileFilter fileFilter = FileFilterUtils.fileFileFilter();
// Use the filter to list all files (not directories) in the current directory
File dir = new File(".");
File[] files = dir.listFiles(fileFilter);
for (File file : files) {
System.out.println(file.getName());
}
}
}
In this example, the FileFilterUtils.fileFileFilter()
will return a filter that only accepts files.