HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

Paul ([email protected]) ([email protected]) 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 the Author
Jerry Ablan is the manager of Internet/Intranet Software Development at the Chicago Board Options Exchange. When not playing WarCraft II with his friends, Jerry and his brother Dan operate NetGeeks (http://www.netgeeks.com), an Internet consulting firm in Chicago, Illinois. Jerry is the author of "Developing Intranet Applications with Java" and the co-author of the "Web Site Administrator's Survival Guide," both from Sams.net. Jerry can be reached at [email protected].net

	

Listing 1

	//Create a file for the current directory
	File file = new File( "." );

	//Get the file list...
	String s[] = file.list();

	//Print the file list...
	for ( int i = 0; i < s.length; i++ )
		System.out.println( Integer.toString( i + 1 ) + ": " + s[ i ] );

Listing 2

//*****************************************************************
//* ExtensionFilter *
//*****************************************************************
public class ExtensionFilter
implements FilenameFilter
{

	String myExtension;

//*****************************************************************
//* ExtensionFilter *
//*****************************************************************

	public
	ExtensionFilter( String extension )
	{
		myExtension = extension;
	}

//*****************************************************************
//* accept *
//*****************************************************************

	public boolean
	accept( File dir, String name )
	{
		return( name.endsWith( myExtension ) );
	}

}

Listing 3

//*****************************************************************
//* Imports *
//*****************************************************************

import java.io.*;

//*****************************************************************
//* FilterTest *
//*****************************************************************

public class 
FilterTest
{

//*****************************************************************
//* main *
//*****************************************************************

	public static void
	main( String args[] )
	{
		new FilterTest();
	}

//*****************************************************************
//* FilterTest  *
//*****************************************************************

	public
	FilterTest()
	{
	//Create a file for this directory
		File file = new File(".");

	//Get the file list...
		String s1[] = file.list();

	//Print the file list...
		for ( int i = 0; i < s1.length; i++ )
		System.out.println( Integer.toString( i + 1 ) + ": " + s1[ i ] );

	//Get the a list of files that end in .java
		String s2[] = file.list( new ExtensionFilter( ".java" ) );

	//Print the file list...
		for ( int i = 0; i < s2.length; i++ )
		System.out.println( Integer.toString( i + 1 ) + ": " + s2[ i ] );
    }

}


 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.