I have tens or even hundreds of thousands of files to list. I thought this would be a rather straightforward thing to do, as for example, running find -iname "*.abc" | wc -l
runs instantly on my Ubuntu laptop. Unfortunately, the equivalent code in Java based on the good old File API is quite slow.
The reason seems to be that each File
instance contains a lot of metadata, while the find
command is smart enough to ignore everything not strictly needed in its search.
It seems that NIO 2 has some "new" constructs to make our lives better: we have a new Visitor based API and a DirectoryStream API. But they still seem to lag somehow in comparison with find
.
What is the fastest of the fastest approach in Java when all we need is to list (or, let's say to make it simpler at the moment, count) huge quantities of files in a set of folders?
Thanks