Package org.apache.commons.io.filefilter
IOFileFilter
that combines both
FileFilter
and FilenameFilter
.
Besides that the package offers a series of ready-to-use implementations of the IOFileFilter interface including implementation that allow you to combine other such filters.
These filter can be used to list files or in FileDialog
,
for example.
DirectoryFilter | Only accept directories |
PrefixFileFilter | Filter based on a prefix |
SuffixFileFilter | Filter based on a suffix |
NameFileFilter | Filter based on a file name |
WildcardFileFilter | Filter based on wildcards |
AgeFileFilter | Filter based on last modified time of file |
SizeFileFilter | Filter based on file size |
TrueFileFilter | Accept all files |
FalseFileFilter | Accept no files |
NotFileFilter | Applies a logical NOT to an existing filter |
AndFileFilter | Combines two filters using a logical AND |
OrFileFilter | Combines two filter using a logical OR |
Using Classic IO
These boolean FilenameFilters can be nested, to allow arbitrary expressions. For example, here is how one could print all non-directory files in the current directory, starting with "A", and ending in ".java" or ".class":
File dir = new File("."); String[] files = dir.list( new AndFileFilter( new AndFileFilter( new PrefixFileFilter("A"), new OrFileFilter( new SuffixFileFilter(".class"), new SuffixFileFilter(".java") ) ), new NotFileFilter( new DirectoryFileFilter() ) ) ); for (int i=0; i<files.length; i++) { System.out.println(files[i]); }
You can alternatively build a filter tree using the "and", "or", and "not" methods on filters themselves:
File dir = new File("."); String[] files = dir.list( new AndFileFilter( new PrefixFileFilter("A").and( new SuffixFileFilter(".class").or(new SuffixFileFilter(".java"))), new DirectoryFileFilter().not() ) ); for (int i=0; i<files.length; i++) { System.out.println(files[i]); }
This package also contains a utility class: FileFilterUtils. It allows you to use all file filters without having to put them in the import section. Here's how the above example will look using FileFilterUtils:
File dir = new File("."); String[] files = dir.list( FileFilterUtils.andFileFilter( FileFilterUtils.andFileFilter( FileFilterUtils.prefixFileFilter("A"), FileFilterUtils.orFileFilter( FileFilterUtils.suffixFileFilter(".class"), FileFilterUtils.suffixFileFilter(".java") ) ), FileFilterUtils.notFileFilter( FileFilterUtils.directoryFileFilter() ) ) ); for (int i=0; i<files.length; i++) { System.out.println(files[i]); }
Using NIO
You can combine Java file tree walking by using java.nio.file.Files.walk()
APIs with filters:
final Path dir = Paths.get(""); // We are interested in files older than one day final long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000); final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff)); // // Walk one dir Files.walkFileTree(dir, Collections.emptySet(), 1, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getFileList()); // visitor.getPathCounters().reset(); // // Walk dir tree Files.walkFileTree(dir, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getDirList()); System.out.println(visitor.getFileList());
There are a few other goodies in that class so please have a look at the documentation in detail.
-
ClassDescriptionAbstracts the implementation of the
FileFilter
(IO),FilenameFilter
(IO),PathFilter
(NIO) interfaces via our ownIOFileFilter
interface.Filters files based on a cutoff time, can filter either newer files or files equal to or older.AFileFilter
providing conditional AND logic across a list of file filters.This filter acceptsFile
s that can be executed.This filter acceptsFile
s that can be read.This filter acceptsFile
s that can be written to.Defines operations for conditional file filters.This class turns a Java FileFilter or FilenameFilter into an IO FileFilter.This filter acceptsFile
s that are directories.This filter accepts files or directories that are empty.A file filter that always returns false.Accepts only an exactFile
object match.This filter acceptsFile
s that are files (not directories).Useful utilities for working with file filters.This filter acceptsFile
s that are hidden.An interface which brings theFileFilter
,FilenameFilter
,PathFilter
, andPathMatcher
interfaces together.File filter for matching files containing a "magic number".Filters file names for a certain name.This filter produces a logical NOT of the filters specified.AFileFilter
providing conditional OR logic across a list of file filters.Accepts only an exactPath
object match.Delegates matching to aPathMatcher
.A file filter backed by a path visitor.Filters file names for a certain prefix.Filters files using supplied regular expression(s).Filters files based on size, can filter either smaller files or files equal to or larger than a given threshold.Filters files based on the suffix (what the file name ends with).This filter acceptsFile
s that are symbolic links.A file filter that always returns true.Filters files using the supplied wildcards.Builds a newWildcardFileFilter
instance.Deprecated.Use WildcardFileFilter.