Class FileFilterUtils
- java.lang.Object
-
- org.apache.commons.io.filefilter.FileFilterUtils
-
public class FileFilterUtils extends java.lang.Object
Useful utilities for working with file filters. It provides access to most file filter implementations in this package so you don't have to import every class you use.- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description FileFilterUtils()
FileFilterUtils is not normally instantiated.
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static IOFileFilter
ageFileFilter(long cutoffMillis)
Returns a filter that returns true if the file was last modified before or at the specified cutoff time.static IOFileFilter
ageFileFilter(long cutoffMillis, boolean acceptOlder)
Returns a filter that filters files based on a cutoff time.static IOFileFilter
ageFileFilter(java.io.File cutoffReference)
Returns a filter that returns true if the file was last modified before or at the same time as the specified reference file.static IOFileFilter
ageFileFilter(java.io.File cutoffReference, boolean acceptOlder)
Returns a filter that filters files based on a cutoff reference file.static IOFileFilter
ageFileFilter(java.util.Date cutoffDate)
Returns a filter that returns true if the file was last modified before or at the specified cutoff date.static IOFileFilter
ageFileFilter(java.util.Date cutoffDate, boolean acceptOlder)
Returns a filter that filters files based on a cutoff date.static IOFileFilter
and(IOFileFilter... filters)
Returns a filter that ANDs the specified filters.static IOFileFilter
andFileFilter(IOFileFilter filter1, IOFileFilter filter2)
Deprecated.static IOFileFilter
asFileFilter(java.io.FileFilter filter)
Returns anIOFileFilter
that wraps theFileFilter
instance.static IOFileFilter
asFileFilter(java.io.FilenameFilter filter)
Returns anIOFileFilter
that wraps theFilenameFilter
instance.static IOFileFilter
directoryFileFilter()
Returns a filter that checks if the file is a directory.static IOFileFilter
falseFileFilter()
Returns a filter that always returns false.static IOFileFilter
fileFileFilter()
Returns a filter that checks if the file is a file (and not a directory).static java.io.File[]
filter(IOFileFilter filter, java.io.File... files)
Applies anIOFileFilter
to the providedFile
objects.static java.io.File[]
filter(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies anIOFileFilter
to the providedFile
objects.static java.util.List<java.io.File>
filterList(IOFileFilter filter, java.io.File... files)
Applies anIOFileFilter
to the providedFile
objects.static java.util.List<java.io.File>
filterList(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies anIOFileFilter
to the providedFile
objects.static java.util.Set<java.io.File>
filterSet(IOFileFilter filter, java.io.File... files)
Applies anIOFileFilter
to the providedFile
objects.static java.util.Set<java.io.File>
filterSet(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies anIOFileFilter
to the providedFile
objects.static IOFileFilter
magicNumberFileFilter(byte[] magicNumber)
Returns a filter that accepts files that begin with the provided magic number.static IOFileFilter
magicNumberFileFilter(byte[] magicNumber, long offset)
Returns a filter that accepts files that contains the provided magic number at a specified offset within the file.static IOFileFilter
magicNumberFileFilter(java.lang.String magicNumber)
Returns a filter that accepts files that begin with the provided magic number.static IOFileFilter
magicNumberFileFilter(java.lang.String magicNumber, long offset)
Returns a filter that accepts files that contains the provided magic number at a specified offset within the file.static IOFileFilter
makeCVSAware(IOFileFilter filter)
Decorates a filter to make it ignore CVS directories.static IOFileFilter
makeDirectoryOnly(IOFileFilter filter)
Decorates a filter so that it only applies to directories and not to files.static IOFileFilter
makeFileOnly(IOFileFilter filter)
Decorates a filter so that it only applies to files and not to directories.static IOFileFilter
makeSVNAware(IOFileFilter filter)
Decorates a filter to make it ignore SVN directories.static IOFileFilter
nameFileFilter(java.lang.String name)
Returns a filter that returns true if the file name matches the specified text.static IOFileFilter
nameFileFilter(java.lang.String name, IOCase ioCase)
Returns a filter that returns true if the file name matches the specified text.static IOFileFilter
notFileFilter(IOFileFilter filter)
Returns a filter that NOTs the specified filter.static IOFileFilter
or(IOFileFilter... filters)
Returns a filter that ORs the specified filters.static IOFileFilter
orFileFilter(IOFileFilter filter1, IOFileFilter filter2)
Deprecated.static IOFileFilter
prefixFileFilter(java.lang.String prefix)
Returns a filter that returns true if the file name starts with the specified text.static IOFileFilter
prefixFileFilter(java.lang.String prefix, IOCase ioCase)
Returns a filter that returns true if the file name starts with the specified text.static IOFileFilter
sizeFileFilter(long threshold)
Returns a filter that returns true if the file is bigger than a certain size.static IOFileFilter
sizeFileFilter(long threshold, boolean acceptLarger)
Returns a filter that filters based on file size.static IOFileFilter
sizeRangeFileFilter(long minSizeInclusive, long maxSizeInclusive)
Returns a filter that accepts files whose size is >= minimum size and <= maximum size.static IOFileFilter
suffixFileFilter(java.lang.String suffix)
Returns a filter that returns true if the file name ends with the specified text.static IOFileFilter
suffixFileFilter(java.lang.String suffix, IOCase ioCase)
Returns a filter that returns true if the file name ends with the specified text.static java.util.List<IOFileFilter>
toList(IOFileFilter... filters)
Create a List of file filters.static IOFileFilter
trueFileFilter()
Returns a filter that always returns true.
-
-
-
Method Detail
-
ageFileFilter
public static IOFileFilter ageFileFilter(java.util.Date cutoffDate)
Returns a filter that returns true if the file was last modified before or at the specified cutoff date.- Parameters:
cutoffDate
- the time threshold- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
ageFileFilter
public static IOFileFilter ageFileFilter(java.util.Date cutoffDate, boolean acceptOlder)
Returns a filter that filters files based on a cutoff date.- Parameters:
cutoffDate
- the time thresholdacceptOlder
- if true, older files get accepted, if false, newer- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
ageFileFilter
public static IOFileFilter ageFileFilter(java.io.File cutoffReference)
Returns a filter that returns true if the file was last modified before or at the same time as the specified reference file.- Parameters:
cutoffReference
- the file whose last modification time is used as the threshold age of the files- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
ageFileFilter
public static IOFileFilter ageFileFilter(java.io.File cutoffReference, boolean acceptOlder)
Returns a filter that filters files based on a cutoff reference file.- Parameters:
cutoffReference
- the file whose last modification time is used as the threshold age of the filesacceptOlder
- if true, older files get accepted, if false, newer- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
ageFileFilter
public static IOFileFilter ageFileFilter(long cutoffMillis)
Returns a filter that returns true if the file was last modified before or at the specified cutoff time.- Parameters:
cutoffMillis
- the time threshold- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
ageFileFilter
public static IOFileFilter ageFileFilter(long cutoffMillis, boolean acceptOlder)
Returns a filter that filters files based on a cutoff time.- Parameters:
cutoffMillis
- the time thresholdacceptOlder
- if true, older files get accepted, if false, newer- Returns:
- an appropriately configured age file filter
- Since:
- 1.2
- See Also:
AgeFileFilter
-
and
public static IOFileFilter and(IOFileFilter... filters)
Returns a filter that ANDs the specified filters.- Parameters:
filters
- the IOFileFilters that will be ANDed together.- Returns:
- a filter that ANDs the specified filters
- Throws:
java.lang.IllegalArgumentException
- if the filters are null or contain a null value.- Since:
- 2.0
- See Also:
AndFileFilter
-
andFileFilter
@Deprecated public static IOFileFilter andFileFilter(IOFileFilter filter1, IOFileFilter filter2)
Deprecated.Returns a filter that ANDs the two specified filters.- Parameters:
filter1
- the first filterfilter2
- the second filter- Returns:
- a filter that ANDs the two specified filters
- See Also:
and(IOFileFilter...)
,AndFileFilter
-
asFileFilter
public static IOFileFilter asFileFilter(java.io.FileFilter filter)
Returns anIOFileFilter
that wraps theFileFilter
instance.- Parameters:
filter
- the filter to be wrapped- Returns:
- a new filter that implements IOFileFilter
- See Also:
DelegateFileFilter
-
asFileFilter
public static IOFileFilter asFileFilter(java.io.FilenameFilter filter)
Returns anIOFileFilter
that wraps theFilenameFilter
instance.- Parameters:
filter
- the filter to be wrapped- Returns:
- a new filter that implements IOFileFilter
- See Also:
DelegateFileFilter
-
directoryFileFilter
public static IOFileFilter directoryFileFilter()
Returns a filter that checks if the file is a directory.- Returns:
- file filter that accepts only directories and not files
- See Also:
DirectoryFileFilter.DIRECTORY
-
falseFileFilter
public static IOFileFilter falseFileFilter()
Returns a filter that always returns false.- Returns:
- a false filter
- See Also:
FalseFileFilter.FALSE
-
fileFileFilter
public static IOFileFilter fileFileFilter()
Returns a filter that checks if the file is a file (and not a directory).- Returns:
- file filter that accepts only files and not directories
- See Also:
FileFileFilter.INSTANCE
-
filter
public static java.io.File[] filter(IOFileFilter filter, java.io.File... files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting array is a subset of the original file list that matches the provided filter.Set<File> allFiles = ... Set<File> javaFiles = FileFilterUtils.filterSet(allFiles, FileFilterUtils.suffixFileFilter(".java"));
- Parameters:
filter
- the filter to apply to the set of files.files
- the array of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.NullPointerException
- if the filter isnull
orfiles
contains anull
value.- Since:
- 2.0
-
filter
public static java.io.File[] filter(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting array is a subset of the original file list that matches the provided filter.The
Set
returned by this method is not guaranteed to be thread safe.Set<File> allFiles = ... Set<File> javaFiles = FileFilterUtils.filterSet(allFiles, FileFilterUtils.suffixFileFilter(".java"));
- Parameters:
filter
- the filter to apply to the set of files.files
- the array of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.IllegalArgumentException
- if the filter isnull
orfiles
contains anull
value.- Since:
- 2.0
-
filterList
public static java.util.List<java.io.File> filterList(IOFileFilter filter, java.io.File... files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting list is a subset of the original files that matches the provided filter.The
List
returned by this method is not guaranteed to be thread safe.List<File> filesAndDirectories = ... List<File> directories = FileFilterUtils.filterList(filesAndDirectories, FileFilterUtils.directoryFileFilter());
- Parameters:
filter
- the filter to apply to each files in the list.files
- the collection of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.IllegalArgumentException
- if the filter isnull
orfiles
contains anull
value.- Since:
- 2.0
-
filterList
public static java.util.List<java.io.File> filterList(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting list is a subset of the original files that matches the provided filter.The
List
returned by this method is not guaranteed to be thread safe.List<File> filesAndDirectories = ... List<File> directories = FileFilterUtils.filterList(filesAndDirectories, FileFilterUtils.directoryFileFilter());
- Parameters:
filter
- the filter to apply to each files in the list.files
- the collection of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.IllegalArgumentException
- if the filter isnull
- Since:
- 2.0
-
filterSet
public static java.util.Set<java.io.File> filterSet(IOFileFilter filter, java.io.File... files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting set is a subset of the original file list that matches the provided filter.The
Set
returned by this method is not guaranteed to be thread safe.Set<File> allFiles = ... Set<File> javaFiles = FileFilterUtils.filterSet(allFiles, FileFilterUtils.suffixFileFilter(".java"));
- Parameters:
filter
- the filter to apply to the set of files.files
- the collection of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.IllegalArgumentException
- if the filter isnull
orfiles
contains anull
value.- Since:
- 2.0
-
filterSet
public static java.util.Set<java.io.File> filterSet(IOFileFilter filter, java.lang.Iterable<java.io.File> files)
Applies an
IOFileFilter
to the providedFile
objects. The resulting set is a subset of the original file list that matches the provided filter.The
Set
returned by this method is not guaranteed to be thread safe.Set<File> allFiles = ... Set<File> javaFiles = FileFilterUtils.filterSet(allFiles, FileFilterUtils.suffixFileFilter(".java"));
- Parameters:
filter
- the filter to apply to the set of files.files
- the collection of files to apply the filter to.- Returns:
- a subset of
files
that is accepted by the file filter. - Throws:
java.lang.IllegalArgumentException
- if the filter isnull
- Since:
- 2.0
-
magicNumberFileFilter
public static IOFileFilter magicNumberFileFilter(byte[] magicNumber)
Returns a filter that accepts files that begin with the provided magic number.- Parameters:
magicNumber
- the magic number (byte sequence) to match at the beginning of each file.- Returns:
- an IOFileFilter that accepts files beginning with the provided magic number.
- Throws:
java.lang.IllegalArgumentException
- ifmagicNumber
isnull
or is of length zero.- Since:
- 2.0
- See Also:
MagicNumberFileFilter
-
magicNumberFileFilter
public static IOFileFilter magicNumberFileFilter(byte[] magicNumber, long offset)
Returns a filter that accepts files that contains the provided magic number at a specified offset within the file.- Parameters:
magicNumber
- the magic number (byte sequence) to match at the provided offset in each file.offset
- the offset within the files to look for the magic number.- Returns:
- an IOFileFilter that accepts files containing the magic number at the specified offset.
- Throws:
java.lang.IllegalArgumentException
- ifmagicNumber
isnull
, or contains no bytes, oroffset
is a negative number.- Since:
- 2.0
- See Also:
MagicNumberFileFilter
-
magicNumberFileFilter
public static IOFileFilter magicNumberFileFilter(java.lang.String magicNumber)
Returns a filter that accepts files that begin with the provided magic number.- Parameters:
magicNumber
- the magic number (byte sequence) to match at the beginning of each file.- Returns:
- an IOFileFilter that accepts files beginning with the provided magic number.
- Throws:
java.lang.IllegalArgumentException
- ifmagicNumber
isnull
or the empty String.- Since:
- 2.0
- See Also:
MagicNumberFileFilter
-
magicNumberFileFilter
public static IOFileFilter magicNumberFileFilter(java.lang.String magicNumber, long offset)
Returns a filter that accepts files that contains the provided magic number at a specified offset within the file.- Parameters:
magicNumber
- the magic number (byte sequence) to match at the provided offset in each file.offset
- the offset within the files to look for the magic number.- Returns:
- an IOFileFilter that accepts files containing the magic number at the specified offset.
- Throws:
java.lang.IllegalArgumentException
- ifmagicNumber
isnull
or the empty String, or if offset is a negative number.- Since:
- 2.0
- See Also:
MagicNumberFileFilter
-
makeCVSAware
public static IOFileFilter makeCVSAware(IOFileFilter filter)
Decorates a filter to make it ignore CVS directories. Passing innull
will return a filter that accepts everything except CVS directories.- Parameters:
filter
- the filter to decorate, null means an unrestricted filter- Returns:
- the decorated filter, never null
- Since:
- 1.1 (method existed but had a bug in 1.0)
-
makeDirectoryOnly
public static IOFileFilter makeDirectoryOnly(IOFileFilter filter)
Decorates a filter so that it only applies to directories and not to files.- Parameters:
filter
- the filter to decorate, null means an unrestricted filter- Returns:
- the decorated filter, never null
- Since:
- 1.3
- See Also:
DirectoryFileFilter.DIRECTORY
-
makeFileOnly
public static IOFileFilter makeFileOnly(IOFileFilter filter)
Decorates a filter so that it only applies to files and not to directories.- Parameters:
filter
- the filter to decorate, null means an unrestricted filter- Returns:
- the decorated filter, never null
- Since:
- 1.3
- See Also:
FileFileFilter.INSTANCE
-
makeSVNAware
public static IOFileFilter makeSVNAware(IOFileFilter filter)
Decorates a filter to make it ignore SVN directories. Passing innull
will return a filter that accepts everything except SVN directories.- Parameters:
filter
- the filter to decorate, null means an unrestricted filter- Returns:
- the decorated filter, never null
- Since:
- 1.1
-
nameFileFilter
public static IOFileFilter nameFileFilter(java.lang.String name)
Returns a filter that returns true if the file name matches the specified text.- Parameters:
name
- the file name- Returns:
- a name checking filter
- See Also:
NameFileFilter
-
nameFileFilter
public static IOFileFilter nameFileFilter(java.lang.String name, IOCase ioCase)
Returns a filter that returns true if the file name matches the specified text.- Parameters:
name
- the file nameioCase
- how to handle case sensitivity, null means case-sensitive- Returns:
- a name checking filter
- Since:
- 2.0
- See Also:
NameFileFilter
-
notFileFilter
public static IOFileFilter notFileFilter(IOFileFilter filter)
Returns a filter that NOTs the specified filter.- Parameters:
filter
- the filter to invert- Returns:
- a filter that NOTs the specified filter
- See Also:
NotFileFilter
-
or
public static IOFileFilter or(IOFileFilter... filters)
Returns a filter that ORs the specified filters.- Parameters:
filters
- the IOFileFilters that will be ORed together.- Returns:
- a filter that ORs the specified filters
- Throws:
java.lang.IllegalArgumentException
- if the filters are null or contain a null value.- Since:
- 2.0
- See Also:
OrFileFilter
-
orFileFilter
@Deprecated public static IOFileFilter orFileFilter(IOFileFilter filter1, IOFileFilter filter2)
Deprecated.Returns a filter that ORs the two specified filters.- Parameters:
filter1
- the first filterfilter2
- the second filter- Returns:
- a filter that ORs the two specified filters
- See Also:
or(IOFileFilter...)
,OrFileFilter
-
prefixFileFilter
public static IOFileFilter prefixFileFilter(java.lang.String prefix)
Returns a filter that returns true if the file name starts with the specified text.- Parameters:
prefix
- the file name prefix- Returns:
- a prefix checking filter
- See Also:
PrefixFileFilter
-
prefixFileFilter
public static IOFileFilter prefixFileFilter(java.lang.String prefix, IOCase ioCase)
Returns a filter that returns true if the file name starts with the specified text.- Parameters:
prefix
- the file name prefixioCase
- how to handle case sensitivity, null means case-sensitive- Returns:
- a prefix checking filter
- Since:
- 2.0
- See Also:
PrefixFileFilter
-
sizeFileFilter
public static IOFileFilter sizeFileFilter(long threshold)
Returns a filter that returns true if the file is bigger than a certain size.- Parameters:
threshold
- the file size threshold- Returns:
- an appropriately configured SizeFileFilter
- Since:
- 1.2
- See Also:
SizeFileFilter
-
sizeFileFilter
public static IOFileFilter sizeFileFilter(long threshold, boolean acceptLarger)
Returns a filter that filters based on file size.- Parameters:
threshold
- the file size thresholdacceptLarger
- if true, larger files get accepted, if false, smaller- Returns:
- an appropriately configured SizeFileFilter
- Since:
- 1.2
- See Also:
SizeFileFilter
-
sizeRangeFileFilter
public static IOFileFilter sizeRangeFileFilter(long minSizeInclusive, long maxSizeInclusive)
Returns a filter that accepts files whose size is >= minimum size and <= maximum size.- Parameters:
minSizeInclusive
- the minimum file size (inclusive)maxSizeInclusive
- the maximum file size (inclusive)- Returns:
- an appropriately configured IOFileFilter
- Since:
- 1.3
- See Also:
SizeFileFilter
-
suffixFileFilter
public static IOFileFilter suffixFileFilter(java.lang.String suffix)
Returns a filter that returns true if the file name ends with the specified text.- Parameters:
suffix
- the file name suffix- Returns:
- a suffix checking filter
- See Also:
SuffixFileFilter
-
suffixFileFilter
public static IOFileFilter suffixFileFilter(java.lang.String suffix, IOCase ioCase)
Returns a filter that returns true if the file name ends with the specified text.- Parameters:
suffix
- the file name suffixioCase
- how to handle case sensitivity, null means case-sensitive- Returns:
- a suffix checking filter
- Since:
- 2.0
- See Also:
SuffixFileFilter
-
toList
public static java.util.List<IOFileFilter> toList(IOFileFilter... filters)
Create a List of file filters.- Parameters:
filters
- The file filters- Returns:
- The list of file filters
- Throws:
java.lang.NullPointerException
- if the filters are null or contain a null value.- Since:
- 2.0
-
trueFileFilter
public static IOFileFilter trueFileFilter()
Returns a filter that always returns true.- Returns:
- a true filter
- See Also:
TrueFileFilter.TRUE
-
-