Java Stream How to - Read text file lines to Stream








Question

We would like to know how to read text file lines to Stream.

Answer

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/* w ww  .j a  va  2s .com*/
public class Main {
   public static void main(String[] args) {
     try {
      Stream<String> lines = Files.lines(Paths.get("files", "data.csv")) ;
    } catch (IOException e) {
      e.printStackTrace();
    }
   }

}