ListFiles performance vs file system visitor
Alan Bateman
Alan.Bateman at Sun.COM
Tue Feb 17 07:27:50 PST 2009
Paulo Levi wrote:
> Hi. I saw recently that the nio2 was going to have a new visitor like
> implementation for filesystem walking
Yes, the method you want is Files#walkFileTree. It's not really possible
to do recursive operations effectively with java.io.File today, esp.
when there are symbolic links.
> and i am wondering if is faster
> than doing a recursive list files by hand. I'm my application this is
> the main bottleneck.
>
Probably not because a hand-rolled file tree walker is going to do the
same things that walkFileTree does. That said, there is one optimization
that we can do on Windows to partly workaround the performance issues
that are often reported on FAT32 and SMB.
> Also besides this question, i hope you don't think it too cheeky for
> me to post my function here for advice. Windows takes about 6 seconds
> find the files (in the explorer given a * regex) , java 49 seconds,
> This on a usb port. On a usb 2 port times become 3s windows and 21
> seconds java.
>
That is a significant difference and probably explained by usage of
isDirectory to check if each file is a directory. If I understand
correctly, then the following code fragment is probably close to what
you want:
static List<Path> findMySource(Path start) {
final List<Path> results = new ArrayList<Path>();
final PathMatcher matcher =
start.getFileSystem().getPathMatcher("glob:*.java");
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
if (matcher.matches(file.getName()))
results.add(file);
return FileVisitResult.CONTINUE;
}
});
return results;
}
-Alan.
More information about the nio-discuss
mailing list