I want to return a stream of paths (these are files located in a certain directory). My initial approach was this:
DirectoryStream getFiles(Path dir) throws IOException {
Files.newDirectoryStream(dir);
}
... but, I would like to know the difference between the above snippet and this second one:
Stream<Path> getFiles(Path dir) throws IOException {
Spliterator<Path> spl = Files.newDirectoryStream(dir).spliterator();
return StreamSupport.stream(spl, false);
}
Both DirectoryStream
and Stream
are sub-interfaces of AutoCloseable
, but beyond that, they seem to be designed for different purposes.
To be more precise, my question is this:
What are the conceptual and functionality-based differences between DirectoryStream
and Stream
interfaces in Java-8?