I am aware that Files.list(Path) uses Files.newDirectoryStream(Path) internally and basically just wraps the DirectoryStream.
However I don\'t understand, when
This is in general a matter of style. If you want to use external iteration (for(Path path : dirStream)
) use newDirectoryStream
. If you want to take the advantage of Stream API operations (like map
, filter
, sorted
, etc.), use list
instead.
The difference is the exception handling. Any exceptions occurred during the Files.list
traversal are converted from DirectoryIteratorException
to UncheckedIOException
. Another minor difference is that the spliterator explicitly reports the DISTINCT
characteristic, so if you do Files.list().distinct()
, the distinct()
step will be optimized out (as it's already known that the elements are distinct). This optimization will not be performed when using Iterable.spliterator()
default implementation.
Nothing special here. You should expect that UncheckedIOException
may pop in the middle of terminal stream operation execution (for example, due to network timeout when accessing the network folder).