Reputation: 15158
I need to find all files in a directory and all of its subdirectories (except some).
Currently I'm using this method:
public static Collection<File> listFiles(File directory,FilenameFilter filter,boolean recurse){
Vector<File> files = new Vector<File>();
File[] entries = directory.listFiles();
if(entries!=null){
for (File entry : entries){
if (filter == null || filter.accept(directory, entry.getName())){
files.add(entry);
}
if (recurse && entry.isDirectory()){
files.addAll(listFiles(entry, filter, recurse));
}
}
}
return files;
}
and using it like this:
this.foundFiles=listFiles(new File(this.BaseDirectory), new FilenameFilter() {
public boolean accept(File dir, String name) {
boolean res=true;
if(name.endsWith(".pdf")){
if(!dir.getPath().endsWith("done")){
if((workingFile!=null && (dir.getPath().equals(workingFile.getParent()) && name.equals(workingFile.getName())))){
res=false;
}else{
try {
ArrayList<String> AuthFolders = DB.getGroupAuthFoldersArray();
for(String folder:AuthFolders){
if(dir.getPath().startsWith(BaseDirectory+File.separator+folder)){
res=true;
break;
}else{
res=false;
}
}
} catch (SQLException ex) {
Logger.getLogger(scanner.class.getName()).log(Level.SEVERE, null, ex);
res=false;
} catch (InterruptedException ex) {
Logger.getLogger(scanner.class.getName()).log(Level.SEVERE, null, ex);
res=false;
}
}
}else{
res=false;
}
}else{
res=false;
}
return res;
}
}, true);
But this is too slow! I have about 3000 files in the directory and it takes 10-15 mins (!!) for this method to find them all.
How Can I do this fast?
I'm thinking about using org.apache.commons.io.FileUtils.listfiles
method. Is there a faster way?
Thanks
Upvotes: 3
Views: 5721
Reputation: 168825
} catch (SQLException ex) {
This does not belong in a class intended to do a directory listing!
Upvotes: 1
Reputation: 274542
Use Files.walkFileTree
in Java 7, which is faster than listFiles
because it uses streaming. Read the tutorial for more information.
Upvotes: 9