Wild Card Filter
Example
See below

About
This is utility code for use by Java programmers; Java provides an abstract class called a FilenameFilter and is used when you want to use wildcard characters when specifying filenames.

Examples:
*.java
in*.log
data*.*

No implementation of this class is provided in the JDK. This program provides a sample of how you might code a FilenameFilter.

JDK Bug

The FilenameFilter was designed to be used with a FileDialog. Unfortunately, this is not supported on most platforms. i.e. A FilenameFilter will not work with a FileDialog. This has been listed as Bug ID 4031440 on the Java Developer Connection site (you need to be a registered user to visit here).

Extract from Sun's response to this bug:

The main issue is that support for FilenameFilter in the FileDialog class was never implemented on any platform -- it's not that there's a bug which needs to be fixed, but that there's no code to run nor was the design ever evaluated to see if it *could* be implemented on our target platforms. ... Unfortunately this is not going to get addressed for 1.2, other than having people use Swing's JFileChooser.

You may download simple hack (fdhack.java) which I understand will work on some platforms. This uses the call such as

fd.setFile("*.java");

(where fd is a FileDialog) to list all files ending with '.java'. No FilenameFilter is used here.


Source
Java Files Class Files Zip Files
JZOOWildCardFilter.java
dir.java
JZOOWildCardFilter.class
dir.class
jzoowildcardfilter.zip
Copy the .java and .class files to a directory and type:

java dir "*.java"

(for example) to run the sample program.

Parameters
None

Usage Example
import java.io.File;

public class dir
{
    public static void main(String[] args)
{ // check args if (args.length != 1) { System.out.println("usage: java dir \"<wildcard>\""); return; } JZOOWildCardFilter filter = new JZOOWildCardFilter(args[0]); System.out.println("Internal structure:"); System.out.println(" toString: " + filter.toString()); System.out.println(" toPattern: " + filter.toPattern()); System.out.println("Directory listing:"); File currDir = new File("."); String files[] = currDir.list(filter); if (files.length == 0) { System.out.println(" No files match " + filter); } else { for (int i=0; i<files.length; i++) { System.out.println(" " + files[i]); } } } }
This example is found in the file dir.java



Last Updated 13 Feb 1999
Copyright © Kevan Stannard 1999

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United states and other countries. JZOO is independent of Sun Microsystems, Inc.