FileNameExtensionFilterpublic final class FileNameExtensionFilter extends FileFilter An implementation of {@code FileFilter} that filters using a
specified set of extensions. The extension for a file is the
portion of the file name after the last ".". Files whose name does
not contain a "." have no file name extension. File name extension
comparisons are case insensitive.
The following example creates a
{@code FileNameExtensionFilter} that will show {@code jpg} files:
FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
JFileChooser fileChooser = ...;
fileChooser.addChoosableFileFilter(filter);
|
Fields Summary |
---|
private final String | description | private final String[] | extensions | private final String[] | lowerCaseExtensions |
Constructors Summary |
---|
public FileNameExtensionFilter(String description, String extensions)Creates a {@code FileNameExtensionFilter} with the specified
description and file name extensions. The returned {@code
FileNameExtensionFilter} will accept all directories and any
file with a file name extension contained in {@code extensions}.
if (extensions == null || extensions.length == 0) {
throw new IllegalArgumentException(
"Extensions must be non-null and not empty");
}
this.description = description;
this.extensions = new String[extensions.length];
this.lowerCaseExtensions = new String[extensions.length];
for (int i = 0; i < extensions.length; i++) {
if (extensions[i] == null || extensions[i].length() == 0) {
throw new IllegalArgumentException(
"Each extension must be non-null and not empty");
}
this.extensions[i] = extensions[i];
lowerCaseExtensions[i] = extensions[i].toLowerCase(Locale.ENGLISH);
}
|
Methods Summary |
---|
public boolean | accept(java.io.File f)Tests the specified file, returning true if the file is
accepted, false otherwise. True is returned if the extension
matches one of the file name extensions of this {@code
FileFilter}, or the file is a directory.
if (f != null) {
if (f.isDirectory()) {
return true;
}
// NOTE: we tested implementations using Maps, binary search
// on a sorted list and this implementation. All implementations
// provided roughly the same speed, most likely because of
// overhead associated with java.io.File. Therefor we've stuck
// with the simple lightweight approach.
String fileName = f.getName();
int i = fileName.lastIndexOf('.");
if (i > 0 && i < fileName.length() - 1) {
String desiredExtension = fileName.substring(i+1).
toLowerCase(Locale.ENGLISH);
for (String extension : lowerCaseExtensions) {
if (desiredExtension.equals(extension)) {
return true;
}
}
}
}
return false;
| public java.lang.String | getDescription()The description of this filter. For example: "JPG and GIF Images."
return description;
| public java.lang.String[] | getExtensions()Returns the set of file name extensions files are tested against.
String[] result = new String[extensions.length];
System.arraycopy(extensions, 0, result, 0, extensions.length);
return result;
| public java.lang.String | toString()Returns a string representation of the {@code FileNameExtensionFilter}.
This method is intended to be used for debugging purposes,
and the content and format of the returned string may vary
between implementations.
return super.toString() + "[description=" + getDescription() +
" extensions=" + java.util.Arrays.asList(getExtensions()) + "]";
|
|