Thursday, 22 August 2013

Listing Files in a Directory On Demand in Java

Listing Files in a Directory On Demand in Java

I have a directory with over 100K files in it, and I want to execute some
function for each file. Right now I'm using File.listFiles() to do this,
but this is extremely inefficient because:
All file names must be read in before any processing occurs, causing an
unnecessarily long hang.
All file names end up being placed into an array, taking up huge amounts
of memory. At any given time I only need enough memory to store one
filename, but here I always need enough memory to store all filenames.
What I really want is something that behaves like a UNIX directory handle,
but I couldn't find anything like this. I also looked up the exactly how
File.listFiles() in OpenJDK, but it ultimately ends up at a native
function call for UNIX-based systems (line 268) and also for Windows (line
525). Worse yet, the native calls are expected to return arrays.
I'd like to avoid plugging into the JNI or calling an external program, if
possible.

No comments:

Post a Comment