How can i filter more than one extension, FileNameFilter only allow one string at a time..
FileNameFilter API
// filter extension, but only allow filtering single extension.
// i want to filter three extension like mp4, flv, wmv
public class GenericExtFilter implements FilenameFilter {
private String ext;
public GenericExtFilter(String ext) {
this.ext = ext;
}
public boolean accept(File dir, String name) {
return (name.endsWith(ext));
}
Rewrite your filter as below.
public class GenericExtFilter implements FilenameFilter {
private String[] exts;
public GenericExtFilter(String... exts) {
this.exts = exts;
}
@Override
public boolean accept(File dir, String name) {
for (String ext : exts) {
if (name.endsWith(ext)) {
return true;
}
}
return false;
}
}
Then you can initialize it with multiple extensions: new GenericExtFilter("mp4", "flv", "wmv");.
The accept method will loop over the array, and if it finds a matching extension will return true, if not, false.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With