Forum Stats

  • 3,826,761 Users
  • 2,260,705 Discussions


New Stream API and I/O streams - Any overlap?

odoepner Member Posts: 2
edited Mar 25, 2014 3:07PM in Java 8 Questions

I know the new Stream API is for processing object streams, often from collections.

But is this notion applicable to I/O streams, too?

It definitely seems compelling to use a functional approach to I/O streams that supports parallelism.

Let me know if my question doesn't make any sense to you




  • James_D
    James_D Member Posts: 1,496 Gold Trophy
    edited Mar 25, 2014 3:08PM

    Indeed, there is some interplay between the APIs.

    For example BufferedReader defines a lines() method that returns a (lazily-read) Stream<String>, each element being a line. The Files class in java.nio.file has a utility lines(Path path) method.

    So assuming you have some class MyClass, some MyClassParser with a parse method, you can use idioms like

    void processFile(Path path) throws IOException {
        MyClassParser parser = ... ;
        try (Stream<String> lines = Files.lines(path)) {
   myObject -> ...);

    If you want to parallelize it, do


    (assuming your parser.parse method can handle multiple threads)