File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Reading a max number of files from a dir Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Reading a max number of files from a dir" Watch "Reading a max number of files from a dir" New topic

Reading a max number of files from a dir

Oliver Plow

Joined: May 18, 2012
Posts: 4

I have a directory with a large number of files (several 10,000 files). I want to process them chunk-wise and therefore want to read them in chunk by chunk. However, when I use File.list(...) or from Apache Commons FileUtils.iterateFiles(...) the respective function iterates over all files also when I have collected the files for my next chunk already which is a waste of CPU time. When my chunk size has been reached I simply return straight away false from within the FileFileter.accept method. Nevertheless, this does not prevent from all the remaining files I'm not interested in any more for the current chunk to be iterated over. And if the number if Files in the dir is large this may take some time...

Does anybody know of some library hat will stop iterating over the files in a directory when some max value has been reached? I couldn't find anything and spent quite some time surfing the Internet.

Thanks, Oliver
James Dickson

Joined: May 18, 2012
Posts: 2
Maybe it's worth looking into Spring Batch ? There might be a learning curve involved though :P
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46337
Welcome to the Ranch both of you.

Doesn’t the File class return the contents of a directory as an array? In which case, you can iterate part of an array with a for loop (not for‑each).
Rob Spoor

Joined: Oct 27, 2005
Posts: 20271

If you're using Java 7, using a DirectoryStream may be an alternative. It (probably) doesn't retrieve all of the files and sub folders into an array, so it may be a bit more efficient. Just use the following example:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: Reading a max number of files from a dir
jQuery in Action, 3rd edition