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()`](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#listFiles%28%29) to do this, but this is extremely inefficient because:

1. All file names must be read in before any processing occurs, causing an unnecessarily long hang.
2. 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()`](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#listFiles%28%29) in OpenJDK, but it ultimately ends up at a [native function call for UNIX-based systems (line 268)](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/io/UnixFileSystem.java#UnixFileSystem) and also [for Windows (line 525)](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/io/Win32FileSystem.java#Win32FileSystem). 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.