Java 8 Streams make it simple to read a text file and print the contents to your console. The following example uses Java 8 Streams to read a file line by line and print to the console.

simple-lines.txt
this
is
a
test
file


PrintFileContents.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package io.nibs.java;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class PrintFileContents {

    public static void main(String args[]) throws IOException {
        final String fileName = "./test-files/simple-lines.txt";

        Files.lines(Paths.get(fileName))
            .forEach(System.out::println);
    }

}


console output
1
2
3
4
5
this
is
a
test
file



If you are new to Java 8 Streams be sure to check out our Introduction to Streams