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:
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.
If you are using Java 7, Nio2's new Path get the files of a directory as a Stream (like an iterator)
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path file: stream) {
System.out.println(file.getFileName());
}
} catch (IOException | DirectoryIteratorException x) {
// IOException can never be thrown by the iteration.
// In this snippet, it can only be thrown by newDirectoryStream.
System.err.println(x);
}
Check out the tutorial : http://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir