Home / JAVA Complete Reference by Examples / Streams / How to read a file line by line using stream in Java 8 by Example
How to read a file line by line using stream in Java 8 by Example
1907 views.
package com.learnbyexamples.streams;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class E005_FileIntoStreamLines {
    public static void main(String[] args) {
        //Files.lines convert files into Stream<String>
        try (Stream<String> lines = Files.lines(Paths.get("names.txt"))) {
            
            //Iterate and Print all lines.
            lines.forEach(System.out::println);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}
Output
raja
gopal
siva
ram
Related Examples
   How to filter an List stream using Lambda expression in Java 8 by Example
   How to filter an Array stream using Lambda expression in Java 8 by Example
   How to filter an Map stream using Lambda expression in Java 8 by Example
   How to use Stream generate method using Lamdba expression in Java 8 Example
   How to read a file line by line using stream in Java 8 by Example
   How to split string into stream in Java 8 by Example
   How to transform stream elements using map in Java 8 by Example
   How to use flatMap stream method in Java 8 by Example
Copyright © 2016 Learn by Examples, All rights reserved