This package defines an interface (IOFileFilter) that combines both
java. and java.. 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 java.,
for example.
There are a number of 'primitive' filters:
| DirectoryFilter | Only accept directories |
| PrefixFileFilter | Filter based on a prefix |
| SuffixFileFilter | Filter based on a suffix |
| NameFileFilter | Filter based on a filename |
| WildcardFileFilter | Filter based on wildcards |
| AgeFileFilter | Filter based on last modified time of file |
| SizeFileFilter | Filter based on file size |
And there are five 'boolean' filters:
| 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 |
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]);
}
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]);
}
There are a few other goodies in that class so please have a look at the documentation in detail.
| Modifier and Type | Interface and Description |
|---|---|
| public interface | ConditionalFileFilter
Defines operations for conditional file filters. |
| public interface | IOFileFilter
An interface which brings the FileFilter and FilenameFilter interfaces together. |
| Modifier and Type | Class and Description |
|---|---|
| public abstract class | AbstractFileFilter
An abstract class which implements the Java FileFilter and FilenameFilter interfaces via the IOFileFilter interface. |
| public class | AgeFileFilter
Filters files based on a cutoff time, can filter either newer files or files equal to or older. |
| public class | AndFileFilter
A |
| public class | CanReadFileFilter
This filter accepts |
| public class | CanWriteFileFilter
This filter accepts |
| public class | DelegateFileFilter
This class turns a Java FileFilter or FilenameFilter into an IO FileFilter. |
| public class | DirectoryFileFilter
This filter accepts |
| public class | EmptyFileFilter
This filter accepts files or directories that are empty. |
| public class | FalseFileFilter
A file filter that always returns false. |
| public class | FileFileFilter
This filter accepts |
| public class | FileFilterUtils
Useful utilities for working with file filters. |
| public class | HiddenFileFilter
This filter accepts |
| public class | NameFileFilter
Filters filenames for a certain name. |
| public class | NotFileFilter
This filter produces a logical NOT of the filters specified. |
| public class | OrFileFilter
A |
| public class | PrefixFileFilter
Filters filenames for a certain prefix. |
| public class | SizeFileFilter
Filters files based on size, can filter either smaller files or files equal to or larger than a given threshold. |
| public class | SuffixFileFilter
Filters files based on the suffix (what the filename ends with). |
| public class | TrueFileFilter
A file filter that always returns true. |
| public class | WildcardFileFilter
Filters files using the supplied wildcards. |
| public class | WildcardFilter
Deprecated
Use WilcardFileFilter. Deprecated as this class performs directory
filtering which it shouldn't do, but that can't be removed due to compatability.
Filters files using the supplied wildcards.
|