Comments
bruce.armstrong wrote: Somebody just said it better than I did, and with more chops to say it: Open Letter to Mark Zuckerberg, Sheryl Sandberg & Facebook Mobile
Cloud Expo on Google News

SYS-CON.TV
Cloud Expo & Virtualization 2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts
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.

About 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).

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

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


Your Feedback
Jeremy Singer wrote: The article mentioned listings that didn't show up for me. The excersise is left for the student.
Latest Cloud Developer Stories
In his session at the 10th International Cloud Expo, Marvin Wheeler, Open Data Center Alliance Chairman, will discuss the success the organization has had in charting the requirements for broad-scale enterprise adoption of the cloud and how 2012 is forecast to be the tipping poin...
Deploying the right technology solutions can significantly increase a company’s productivity and profitability. However, for most companies today, lowering the total cost of ownership (TCO) of their technology infrastructure is a top priority and they’re now evaluating cloud comp...
For many of the same reasons that Software-as-a-Service is catching on with enterprise buyers, delivering web services on top of Infrastructure-as-a-Service architectures is appealing to the SaaS developers. Operational agility, lower CapEx, and a broad array of tools and service...
With Cloud Expo 2012 New York (10th Cloud Expo) now less than three weeks away, what better time to introduce you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical...
Cloud computing already has trimmed some businesses' IT costs. But a report found that it also could be the next big thing to help reduce their energy use, according to greenbiz.com The fourth annual Energy Efficient IT Report – by technology products and services seller CDW – c...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE

Breaking Cloud Computing News

Alibaba Group Holding Limited (“Alibaba Group”) and Alibaba.com Limited (HKSE: 1688...