WildcardFileFilterpublic class WildcardFileFilter extends AbstractFileFilter implements SerializableFilters 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]);
}
|
Fields Summary |
---|
private final String[] | wildcardsThe wildcards that will be used to match filenames. | private final IOCase | caseSensitivityWhether the comparison is case sensitive. |
Constructors Summary |
---|
public WildcardFileFilter(String wildcard)Construct a new case-sensitive wildcard filter for a single wildcard.
this(wildcard, null);
| public WildcardFileFilter(String wildcard, IOCase caseSensitivity)Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
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.
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.
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.
this(wildcards, null);
| public WildcardFileFilter(List wildcards, IOCase caseSensitivity)Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
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 boolean | accept(java.io.File dir, java.lang.String name)Checks to see 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 boolean | accept(java.io.File file)Checks to see 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.String | toString()Provide a String representaion of this file filter.
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();
|
|