FileDocCategorySizeDatePackage
WildcardFilter.javaAPI DocAndroid 1.5 API4993Wed May 06 22:42:46 BST 2009org.apache.commons.io.filefilter

WildcardFilter

public class WildcardFilter extends AbstractFileFilter implements Serializable
Filters files using the supplied wildcards.

This filter selects files, but not directories, based on one or more wildcards and using case-sensitive comparison.

The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines. The extension check is case-sensitive. See {@link FilenameUtils#wildcardMatch} for more information.

For example:

File dir = new File(".");
FileFilter fileFilter = new WildcardFilter("*test*.java~*~");
File[] files = dir.listFiles(fileFilter);
for (int i = 0; i < files.length; i++) {
System.out.println(files[i]);
}
author
Jason Anderson
version
$Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
since
Commons IO 1.1
deprecated
Use WilcardFileFilter. Deprecated as this class performs directory filtering which it shouldn't do, but that can't be removed due to compatability.

Fields Summary
private final String[]
wildcards
The wildcards that will be used to match filenames.
Constructors Summary
public WildcardFilter(String wildcard)
Construct a new case-sensitive wildcard filter for a single wildcard.

param
wildcard the wildcard to match
throws
IllegalArgumentException if the pattern is null

        if (wildcard == null) {
            throw new IllegalArgumentException("The wildcard must not be null");
        }
        this.wildcards = new String[] { wildcard };
    
public WildcardFilter(String[] wildcards)
Construct a new case-sensitive wildcard filter for an array of wildcards.

param
wildcards the array of wildcards to match
throws
IllegalArgumentException if the pattern array is null

        if (wildcards == null) {
            throw new IllegalArgumentException("The wildcard array must not be null");
        }
        this.wildcards = wildcards;
    
public WildcardFilter(List wildcards)
Construct a new case-sensitive wildcard filter for a list of wildcards.

param
wildcards the list of wildcards to match
throws
IllegalArgumentException if the pattern list is null
throws
ClassCastException if the list does not contain Strings

        if (wildcards == null) {
            throw new IllegalArgumentException("The wildcard list must not be null");
        }
        this.wildcards = (String[]) wildcards.toArray(new String[wildcards.size()]);
    
Methods Summary
public booleanaccept(java.io.File dir, java.lang.String name)
Checks to see if the filename matches one of the wildcards.

param
dir the file directory
param
name the filename
return
true if the filename matches one of the wildcards

        if (dir != null && new File(dir, name).isDirectory()) {
            return false;
        }
        
        for (int i = 0; i < wildcards.length; i++) {
            if (FilenameUtils.wildcardMatch(name, wildcards[i])) {
                return true;
            }
        }
        
        return false;
    
public booleanaccept(java.io.File file)
Checks to see if the filename matches one of the wildcards.

param
file the file to check
return
true if the filename matches one of the wildcards

        if (file.isDirectory()) {
            return false;
        }
        
        for (int i = 0; i < wildcards.length; i++) {
            if (FilenameUtils.wildcardMatch(file.getName(), wildcards[i])) {
                return true;
            }
        }
        
        return false;