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

WildcardFileFilter

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

This filter selects files and directories based on one or more wildcards. Testing is case-sensitive by default, but this can be configured.

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 by . See {@link FilenameUtils#wildcardMatchOnSystem} for more information.

For example:

File dir = new File(".");
FileFilter fileFilter = new WildcardFileFilter("*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: 155419 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
since
Commons IO 1.3

Fields Summary
private final String[]
wildcards
The wildcards that will be used to match filenames.
private final IOCase
caseSensitivity
Whether the comparison is case sensitive.
Constructors Summary
public WildcardFileFilter(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

        this(wildcard, null);
    
public WildcardFileFilter(String wildcard, IOCase caseSensitivity)
Construct a new wildcard filter for a single wildcard specifying case-sensitivity.

param
wildcard the wildcard to match, not null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the pattern is null

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

The array is not cloned, so could be changed after constructing the instance. This would be inadvisable however.

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

        this(wildcards, null);
    
public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity)
Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.

The array is not cloned, so could be changed after constructing the instance. This would be inadvisable however.

param
wildcards the array of wildcards to match, not null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the pattern array is null

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

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

        this(wildcards, null);
    
public WildcardFileFilter(List wildcards, IOCase caseSensitivity)
Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.

param
wildcards the list of wildcards to match, not null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
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()]);
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
    
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

        for (int i = 0; i < wildcards.length; i++) {
            if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
                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

        String name = file.getName();
        for (int i = 0; i < wildcards.length; i++) {
            if (FilenameUtils.wildcardMatch(name, wildcards[i], caseSensitivity)) {
                return true;
            }
        }
        return false;
    
public java.lang.StringtoString()
Provide a String representaion of this file filter.

return
a String representaion

        StringBuffer buffer = new StringBuffer();
        buffer.append(super.toString());
        buffer.append("(");
        if (wildcards != null) {
            for (int i = 0; i < wildcards.length; i++) {
                if (i > 0) {
                    buffer.append(",");
                }
                buffer.append(wildcards[i]);
            }
        }
        buffer.append(")");
        return buffer.toString();