Jerry Ablan

Subscribe to Jerry Ablan: eMailAlertsEmail Alerts
Get Jerry Ablan: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


Article

The Mysterious FilenameFilter Class

The Mysterious FilenameFilter Class

Paul (paul@public.wh.hb.cn) (lweiden@erols.com) writes:
I can't use File.list( FilenameFilter ). How do you use FilenameFilter?


The java.io.File class is an excellent representation of a file. But what happens if you instantiate a File object with a directory name instead of a file name? Not much, but it does allow you to use the list() method. list()is a very cool method. It retrieves the names of all the files in the directory specified at construction. They are returned to you in a String array. This array can then be used to fill a List, or other AWT component, to present a file list to the user.

Listing 1 opens a directory and retrieves the names of the files in it.

But what if you only want files of a certain type to be in your listing? You can always parse through the returned array and remove the unwanted files. But there is another, easier way! It is the FilenameFilter class.

The FilenameFilter class is an interface that lives in the java.io package. Being an interface requires you to implement it in your own classes. This allows you maximum flexibility when using filters.

The FilenameFilter interface contains a single method:

public boolean
accept( File dir, String name )

dir is the File object that represents the directory that is being listed. name is the actual file name.

The File class contains a second overloaded list() method. This second one accepts a FilenameFilter implementor as an argument. When the list() method builds its array of names, it first calls the accept() method of the FilenameFilter implementor. If the accept() method returns true, the file name is placed into the array. If the accept() method returns false, the file name is discarded. A simple filter! Perhaps now you can see many different possibilities for filtering file names with this class.

Listing 2 is an example filter that I cooked up. It is called ExtensionFilter and it filters files of a certain extension. It is not very robust and is extremely simple. Its purpose is to illustrate the use of the FilenameFilter class.

This class simple stores the extension passed in during construction. You could at this point add error checking and cleanup of the extension string. The accept method implements the FilenameFilter interface and checks the argument name against the extension stored. This is done with the String method endsWith().

So how do you use this puppy? Easy! Below is a FilterTest class that uses the above ExtensionFilter and displays the files in the current directory.

More Stories By Jerry Ablan

Jerry Ablan is the manager of Internet/intranet Software Development at the Chicago Board Options Exchange. Jerry and his brother Dan (dma@mcs.net), operate NetGeeks (http://www.netgeeks.com), an Internet consulting firm in Chicago, Illinois. Jerry is the author of Developing Intranet Applications with Java from Sams.net; the co-author of the Web Site Administrator's Survival Guide from Sams.net. He also was a contributing author to: Special Edition: Using Java (Que); Platinum Edition Using CGI, HTML, and Java (Que); and Intranets Unleashed (Sams.net).

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Jeremy Singer 03/30/05 06:37:12 AM EST

The article mentioned listings that didn't show up for me.
The excersise is left for the student.