PrefixFileFilterpublic class PrefixFileFilter extends AbstractFileFilter implements SerializableFilters filenames for a certain prefix.
For example, to print all files and directories in the
current directory whose name starts with Test :
File dir = new File(".");
String[] files = dir.list( new PrefixFileFilter("Test") );
for ( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}
|
Fields Summary |
---|
private final String[] | prefixesThe filename prefixes to search for | private final IOCase | caseSensitivityWhether the comparison is case sensitive. |
Constructors Summary |
---|
public PrefixFileFilter(String prefix)Constructs a new Prefix file filter for a single prefix.
this(prefix, IOCase.SENSITIVE);
| public PrefixFileFilter(String prefix, IOCase caseSensitivity)Constructs a new Prefix file filter for a single prefix
specifying case-sensitivity.
if (prefix == null) {
throw new IllegalArgumentException("The prefix must not be null");
}
this.prefixes = new String[] {prefix};
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
| public PrefixFileFilter(String[] prefixes)Constructs a new Prefix file filter for any of an array of prefixes.
The array is not cloned, so could be changed after constructing the
instance. This would be inadvisable however.
this(prefixes, IOCase.SENSITIVE);
| public PrefixFileFilter(String[] prefixes, IOCase caseSensitivity)Constructs a new Prefix file filter for any of an array of prefixes
specifying case-sensitivity.
The array is not cloned, so could be changed after constructing the
instance. This would be inadvisable however.
if (prefixes == null) {
throw new IllegalArgumentException("The array of prefixes must not be null");
}
this.prefixes = prefixes;
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
| public PrefixFileFilter(List prefixes)Constructs a new Prefix file filter for a list of prefixes.
this(prefixes, IOCase.SENSITIVE);
| public PrefixFileFilter(List prefixes, IOCase caseSensitivity)Constructs a new Prefix file filter for a list of prefixes
specifying case-sensitivity.
if (prefixes == null) {
throw new IllegalArgumentException("The list of prefixes must not be null");
}
this.prefixes = (String[]) prefixes.toArray(new String[prefixes.size()]);
this.caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity);
|
Methods Summary |
---|
public boolean | accept(java.io.File file)Checks to see if the filename starts with the prefix.
String name = file.getName();
for (int i = 0; i < this.prefixes.length; i++) {
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
return true;
}
}
return false;
| public boolean | accept(java.io.File file, java.lang.String name)Checks to see if the filename starts with the prefix.
for (int i = 0; i < prefixes.length; i++) {
if (caseSensitivity.checkStartsWith(name, prefixes[i])) {
return true;
}
}
return false;
| public java.lang.String | toString()Provide a String representaion of this file filter.
StringBuffer buffer = new StringBuffer();
buffer.append(super.toString());
buffer.append("(");
if (prefixes != null) {
for (int i = 0; i < prefixes.length; i++) {
if (i > 0) {
buffer.append(",");
}
buffer.append(prefixes[i]);
}
}
buffer.append(")");
return buffer.toString();
|
|