I want to collect directory structure to a given depth (It can be huge recursive structure!).
Which approach should perform better and consumes less memory?
Approach 1: Using DirectoryStream
OR
Approach 2:
Q. As
java doc says -
* <p> Note that the {@link java.nio.file.Files} class defines the {@link
* java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method
* to open a directory and iterate over the names of the files in the
* directory. This may use less resources when working with very large
* directories.
I am visiting directories in DFS manner(recursive approach), will java.io.File#listFiles perform better than DirectoryStream as stream entries won't be garbage collected till whole structure is visited (not sure on this) causing more memory usages?