File iterator

Does anyone know, why the File class doesn’t provide an iterator method? To loop all the files in a folder I have to use the listFiles() method. Of course I can pass in a FileFilter. But it still seems more efficient to me, if I could get a file iterator from the folder. Theoretically for very large folders it would take a lot of memory and takes unnecessary time to allocate and fill the array. Additionally the listFiles() method first reads a full array of filenames and then derives a File array from it. This is doubly inefficient. And I guess, the code also creates something like an ArrayList, which has to recreate the uderlying array again and again when it is filled.

This is all fine, if I need the files as an array. But if I needs them in a different container or just want to loop over them, this seems to be dumb.

How can I effectively suggest this to Oracle?

Marvin

Based on what? Actual profiler output? Or random guesswork?

Since anything file system related will almost certainly have to go via the host OS, it’s entirely likely that a single listDirContents call is much more efficient than lots of little getNextFile calls, making the actual object and array construction overhead negligible.

See my implementation here:

http://www.java-gaming.org/index.php/topic,21629.0.html

FYI: list(FileFilter) and listFiles(FileFilter) do only invoke the filter when all the files have been gathered.

Nice implementation. And nice coding style :). Some spaces here and there and it is perfect ;).

So Java 7 provides a way to to this out of the box?

Marvin

… yes.