Java 8 Read File Using Buffered Reader

Finally, Java 8 has made Reading & Writing a text file a simple task.

If we need more fine-grained control on reading we can use new Files.newBufferedReader()

Read File Using Buffered Reader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

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

public class Java8ReadUsingBufferedReader {

public static void main(String[] args) throws IOException {
String filePath = "C:\\data\\demo\\sample.txt";
BufferedReader reader = Files.newBufferedReader(Paths.get(filePath));
reader.lines().forEach(System.out::println);

}

}
Sample.txt file
1
2
3
4
5
6
7
public final class Files extends Object
This class consists exclusively of static methods that operate on files, directories, or other types of files.
In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

Since:
1.7

Share Comments