Filter : Java Glossary

Filter
The term has several meanings:

Filtering File.list

You filter File.list by writing a class that implements java.io. FilenameFilter;
String[] filenames = file.list( someFilter );
will get list of files in this directory. Wildcards won’t work. Note it returns an array of Strings, not including the directory, not File objects. Files are not in any particular order. They include the subdirectories, but not the . or .. entries.

Your FilenameFilter needs at a minimum to filter with File.isDirectory().

You can also use FileDialog.setFilenameFilter( java.io.FilenameFilter ) to restrict which files are displayed in a File choosing dialog.

Here is how to write a simple class that implements the FilenameFilter interface:

I have written 10 FilenameFilters with source. All are trivial except for the MultiFilter that lets you combine other filters in various ways.

They come with Java source. The collection is mainly for newbies trying to get some ideas on how FilenameFilters work and how you could write them.

FilenameFilters
Name Purpose
AllDirectoriesFilter gets all directories
AllFilesFilter gets all files
ClamFilter simple wildcard
DirListFilter gets a list of directories
EndsWithFilter gets files whose names end with a given string.
ExtensionFilter gets files matching a list of extensions
EverythingFilter gets everything
FileLengthFilter gets short or long files
FilenameLengthFilter gets short or long filenames
FileListFilter gets a list of files
MultiFilter combines other Filters with "consider", "must" and "never".
RecentFilter filters based on lastModified date
RegexFilter filters with Regex expressions.
StartsWithFilter gets files whose names start with a given string.

You can cascade (combine) FilenameFilters by having one ask advice of another, or write a mother filter that asks the advice of several child filters and && or || the results of their accept methods. MultiFilter makes that task easy.

FileInputStream

Here is rough example how you might use a FileInputStream to monitor progress. You could write yours simply by overriding reportProgress in mine.

Learning More

Oracle’s Javadoc on FilenameFilter class : available:
Oracle’s Javadoc on FileFilter class : available:
Oracle’s Javadoc on File class : available:
Oracle’s Javadoc on FilterInputStream class : available:
Oracle’s Javadoc on FilterOutputStream class : available:

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/filter.html J:\mindprod\jgloss\filter.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, please quote it and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. Threatening to kill me or spouting obscenities has yet to persuade me to change my mind.
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.179.214]
You are visitor number 39,217.