ListFiles performance vs file system visitor

Paulo Levi i30817 at gmail.com
Tue Feb 17 10:01:38 PST 2009


How about adding a iterator like method (to remove the array creation)?

Also if wasn't for the isDirectory i could make a lazy File
extension/delegate (no normalization - if the private constructor was
protected that would work too), at least for the files that aren't
directories (that are in common usage the most of them). Any
alternative to is directory?

Nio2 can't be used as a seperate jar since it introduces new native api right?


On Tue, Feb 17, 2009 at 3:27 PM, Alan Bateman <Alan.Bateman at sun.com> wrote:
> 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