io - Listing Files in a Directory On Demand in Java -


i have directory on 100k files in it, , want execute function each file. right i'm using file.listfiles() this, extremely inefficient because:

  1. all file names must read in before processing occurs, causing unnecessarily long hang.
  2. all file names end being placed array, taking huge amounts of memory. @ given time need enough memory store 1 filename, here need enough memory store filenames.

what want behaves unix directory handle, couldn't find this. looked how file.listfiles() in openjdk, ends @ native function call unix-based systems (line 268) , for windows (line 525). worse yet, native calls expected return arrays.

i'd avoid plugging jni or calling external program, if possible.

if using java 7, nio2's new path files of directory stream (like iterator)

try (directorystream<path> stream = files.newdirectorystream(dir)) {     (path file: stream) {         system.out.println(file.getfilename());     } } catch (ioexception | directoryiteratorexception x) {     // ioexception can never thrown iteration.     // in snippet, can thrown newdirectorystream.     system.err.println(x); } 

check out tutorial : http://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir