2

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?

Chthonic Project
  • 8,216
  • 1
  • 43
  • 92
  • In jdk8 there's `Stream Files.lines(Path dir)` ([javadoc](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#lines-java.nio.file.Path-)) that internally wraps around `DirectoryStream` and lets you do all the new stream operations. You may also want to take a look at the new `Files.walk` for stream-based recursive directory listing. – Misha Apr 14 '15 at 02:04
  • `Files#lines(Path p)` is for streaming the lines in a single file, returning a stream of `String`s ... unless I missed something obvious. – Chthonic Project Apr 14 '15 at 02:08
  • 2
    I meant `Files.list`, sorry. – Misha Apr 14 '15 at 02:13
  • Just saw the source code for `Files#list`. Thank you for pointing it out. I was implementing my own (no doubt inferior) method to do exactly what this method accomplishes. – Chthonic Project Apr 14 '15 at 02:32

1 Answers1

2

What are the conceptual and functionality-based differences between DirectoryStream and Stream interfaces in Java-8?

Java Stream API is general purpose API designed and implemented provide immutable, lazy, functional/declarative style of coding with any stream of objects. This is not specific for one scope and has mechanisms to filter, transform, aggregate data coming from the stream.

Where as DirectoryStream is specifically designed to cater loading, filtering and iterating through file system directories in a easy to use API.

Java Stream API has clear cut common usage functions and corresponding SAM (Single Abstract Method) interfaces to ease coding for almost any usecase.

Where as DirectoryStream has convenient functions and interfaces to carry out loading, filtering, iterating over directories easy.

shazin
  • 21,379
  • 3
  • 54
  • 71
  • I was thinking along these lines as well, but if this was the case, shouldn't `DirectoryStream` be a subinterface of `Stream`? I am especially curious about why one extends `Iterable` while the other doesn't. – Chthonic Project Apr 14 '15 at 02:12
  • 1
    Well if DirectoryStream was a sub interface of Stream, it wouldn't make sense because not all methods of Stream are suitable for DirectoryStream. – shazin Apr 14 '15 at 02:14
  • 3
    @Chthonic Project: you have to keep in mind that these two types were introduced in different Java versions. The design principles of the new `Stream` API were not defined when `DirectoryStream` was introduced. Most probably, if `DirectoryStream` was introduced in Java 8, it didn’t implement `Iterable` for the same reason `Stream` doesn’t, both have a single-use `iterator()` method. Maybe, the `DirectoryStream` also had an API suitable to be a subtype of `Stream` then… – Holger Apr 15 '15 at 19:33