How to read file using Files.newBufferedReader?

In the snippet below you’ll learn to open file for reading using Files.newBufferedReader() method in JDK 7. This method returns a which makes a backward compatibility with the old I/O system in Java.

To read a file you’ll need to provide a Path and the Charset to the newBufferedReader() method arguments.

package org.kodejava.example.nio;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FilesNewBufferedReader {
    public static void main(String[] args) {
        Path logFile = Paths.get("D:\Temp\logs\app.log");
        try (BufferedReader reader = Files.newBufferedReader(logFile,
                StandardCharsets.UTF_8)) {
            String line;
            while ((line = reader.readLine()) != null) {
        } catch (Exception e) {

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.

Leave a Reply