Reputation: 1443
File files[] = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
return true;
String name = file.getName().toLowerCase();
if (name.endsWith(".zip") || name.endsWith(".jar")
|| name.endsWith(".z") || name.endsWith(".gz")
|| name.endsWith(".tar") || name.endsWith(".bz2")
|| name.endsWith(".bz"))
return true;
return false;
}
});
As you can see, the code is dirty with "||"
Do you know how to make it better?
Upvotes: 10
Views: 10461
Reputation: 191875
With Java 6 or above, this is a perfect case for a FileNameExtensionFilter
... except that it extends javax.swing.filechooser.FileFilter
instead of implementing java.io.FileFilter
.
But it is trivial to write a wrapper for it:
File[] files = rootDir.listFiles(new FileFilter() {
private final FileNameExtensionFilter filter =
new FileNameExtensionFilter("Compressed files",
"zip", "jar", "z", "gz", "tar", "bz2", "bz");
public boolean accept(File file) {
return filter.accept(file);
}
});
Upvotes: 17
Reputation: 114767
Here's my approach. java.lang.Collections is really a nice class! And because we're looking up the given file extension in a HashSet, it's more performant. Although I doubt, that performance really matters in this case ...
// ...
final Set<String> archives = new HashSet<String>();
Collections.addAll(archives, ".zip", ".jar", ".z", ".gz", ".tar",
".bz2", ".bz");
File files[] = rootDir.listFiles(new FileFilter() {
public boolean accept(final File file) {
if (file.isDirectory())
return true;
final String name = file.getName().toLowerCase();
return archives.contains(name
.substring(name.lastIndexOf('.')));
}
});
// ...
Upvotes: 0
Reputation: 63662
You could do the following using a statically initialized HashSet. Personally I'd pull the allowed extensions out into some sort of configuration file to make it a bit easier to change, but you don't have to.
n.b. FilenameUtils belongs to Commons I/O which also includes a bunch of classes that make doing this kind of stuff easier. Take a look at FileFilterUtils as well, which simplifies things even further and provides some nice helper methods.
private static Set allowedExtensions = null;
static {
allowedExtensions = new HashSet<String>();
allowedExtensions.add("txt");
allowedExtensions.add("zip");
allowedExtensions.add("jar");
allowedExtensions.add("gz");
}
public void filter() {
File rootDir = new File("/");
File files[] = rootDir.listFiles(new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory()) return true;
String fileName = file.getName().toLowerCase();
String extension = FilenameUtils.getExtension(fileName);
if (StringUtils.isNotEmpty(extension)
&& allowedExtensions.contains(extension)) {
return true;
} else {
return false;
}
}
});
}
You can find the API here:
http://commons.apache.org/io/api-release/
Upvotes: 1
Reputation: 116314
I just finished writing this class:
class FileExtensionFilter implements FileFilter {
private final String[] validExtensions;
public FileExtensionFilter(String... validExtensions) {
this.validExtensions = validExtensions;
}
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
String name = pathname.getName().toLowerCase();
for (String ext : validExtensions) {
if (name.endsWith(ext)) {
return true;
}
}
return false;
}
}
usage:
File files[] = directory.listFiles(
new FileExtensionFilter(".zip", ".jar", ".z", ".tar"));
BTW this is a reusable class, you can even wrap it with additional checks using the decorator patter, etc.
PS
just noticed the existence of FileNameExtensionFilter
Upvotes: 1
Reputation: 126095
Some pseudocode solutions:
suffixes = [".tar", ".zip", ".jpg"]
for suffix in suffixes:
if name.endsWith(suffix):
return True
suffixes = [".tar", ".zip", ".jpg"]
nameSuffix = name.getSuffix()
if nameSuffix in suffixes:
return True
Upvotes: 5
Reputation: 47042
You could statically create a map, and return true if the filename extension is a key in the map.
Or you could try to match the filename against a regular expression (but I'd choose to use a map instead).
Upvotes: 0
Reputation: 19313
Why not use regular expressions?
static final Pattern p = Pattern.compile("\\.(zip|jar|z|gz)$");
and then return p.matcher(name).find();
Upvotes: 5