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

NameFileFilter

public class NameFileFilter extends AbstractFileFilter implements Serializable
Filters filenames for a certain name.

For example, to print all files and directories in the current directory whose name is Test:

File dir = new File(".");
String[] files = dir.list( new NameFileFilter("Test") );
for ( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}
since
Commons IO 1.0
version
$Revision: 606381 $ $Date: 2007-12-22 02:03:16 +0000 (Sat, 22 Dec 2007) $
author
Stephen Colebourne
author
Federico Barbieri
author
Serge Knystautas
author
Peter Donald

Fields Summary
private final String[]
names
The filenames to search for
private final IOCase
caseSensitivity
Whether the comparison is case sensitive.
Constructors Summary
public NameFileFilter(String name)
Constructs a new case-sensitive name file filter for a single name.

param
name the name to allow, must not be null
throws
IllegalArgumentException if the name is null

        this(name, null);
    
public NameFileFilter(String name, IOCase caseSensitivity)
Construct a new name file filter specifying case-sensitivity.

param
name the name to allow, must not be null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the name is null

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

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

param
names the names to allow, must not be null
throws
IllegalArgumentException if the names array is null

        this(names, null);
    
public NameFileFilter(String[] names, IOCase caseSensitivity)
Constructs a new name file filter for an array of names specifying case-sensitivity.

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

param
names the names to allow, must not be null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the names array is null

        if (names == null) {
            throw new IllegalArgumentException("The array of names must not be null");
        }
        this.names = names;
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
    
public NameFileFilter(List names)
Constructs a new case-sensitive name file filter for a list of names.

param
names the names to allow, must not be null
throws
IllegalArgumentException if the name list is null
throws
ClassCastException if the list does not contain Strings

        this(names, null);
    
public NameFileFilter(List names, IOCase caseSensitivity)
Constructs a new name file filter for a list of names specifying case-sensitivity.

param
names the names to allow, must not be null
param
caseSensitivity how to handle case sensitivity, null means case-sensitive
throws
IllegalArgumentException if the name list is null
throws
ClassCastException if the list does not contain Strings

        if (names == null) {
            throw new IllegalArgumentException("The list of names must not be null");
        }
        this.names = (String[]) names.toArray(new String[names.size()]);
        this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
    
Methods Summary
public booleanaccept(java.io.File file)
Checks to see if the filename matches.

param
file the File to check
return
true if the filename matches

        String name = file.getName();
        for (int i = 0; i < this.names.length; i++) {
            if (caseSensitivity.checkEquals(name, names[i])) {
                return true;
            }
        }
        return false;
    
public booleanaccept(java.io.File file, java.lang.String name)
Checks to see if the filename matches.

param
file the File directory
param
name the filename
return
true if the filename matches

        for (int i = 0; i < names.length; i++) {
            if (caseSensitivity.checkEquals(name, names[i])) {
                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 (names != null) {
            for (int i = 0; i < names.length; i++) {
                if (i > 0) {
                    buffer.append(",");
                }
                buffer.append(names[i]);
            }
        }
        buffer.append(")");
        return buffer.toString();