Java Files newBufferedReader() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.newBufferedReader(path) opens the file for reading text and returns a BufferedReader. You then pull text out at your own pace, typically with readLine() until it returns null.

The Java 8 overload decodes UTF-8; the Java 7 overload takes a Charset. It replaces the old new BufferedReader(new FileReader(file)) chain, which used the platform default charset.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.newBufferedReader(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Open in try-with-resources and loop:

Java
try (BufferedReader in = Files.newBufferedReader(path)) {
    String line;
    while ((line = in.readLine()) != null) {
        // use line
    }
}
CallWhat it does
Files.newBufferedReader(p)UTF-8 reader (Java 8+).
Files.newBufferedReader(p, cs)Reader for charset cs (Java 7+).
reader.lines()Java 8 stream of the remaining lines.

Since Java 7 (with charset) and Java 8 (UTF-8 default). The reader recognises \n, \r\n, and \r as line ends.

Library declarations
Java
public static BufferedReader newBufferedReader(Path path) throws IOException
public static BufferedReader newBufferedReader(Path path, Charset cs) throws IOException

Parameters

ParameterDescription
pathThe text file to open.
cs(Java 7 overload) Charset used to decode.

Return Value

Return ValueDescription
BufferedReaderAn open reader positioned at the start of the file. Close it when done.

Compare with Similar Methods

Reading text:

MethodHow it differs
lines()Stream of lines without managing the reader.
readAllLines()Everything at once in a list.
newBufferedWriter()The writing counterpart.

Java Examples

Example 1 — Print a File with Line Numbers

The classic while ((line = readLine()) != null) loop.

Java
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class NewBufferedReaderNumbered {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path poem = dir.resolve("poem.txt");
        Files.write(poem, "Roses are red\nViolets are blue\nJava is fun".getBytes(StandardCharsets.UTF_8));
        try (BufferedReader in = Files.newBufferedReader(poem)) {
            String line;
            int n = 0;
            while ((line = in.readLine()) != null) {
                n++;
                System.out.println(n + ": " + line);
            }
        }
        Files.delete(poem);
        Files.delete(dir);
    }
}

How It Works

  1. Read, test, repeat. The assignment inside the while condition reads a line and checks it in one expression.
  2. null means the end. After "Java is fun" there is nothing left, so readLine() returns null and the loop stops.
  3. Close before deleting. The try-with-resources block closes the reader first, which Windows requires before the file can be deleted.

Example 2 — Load a Simple Settings File into a Map

Skip comments and blank lines, split each remaining line at the first equals sign, and store the pairs in a sorted map.

Java
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.TreeMap;

public class NewBufferedReaderConfig {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path cfg = dir.resolve("server.cfg");
        Files.write(cfg, "# server settings\nport=8080\n\nhost=localhost\n".getBytes(StandardCharsets.UTF_8));
        Map<String, String> settings = new TreeMap<>();
        try (BufferedReader in = Files.newBufferedReader(cfg, StandardCharsets.UTF_8)) {
            String line;
            while ((line = in.readLine()) != null) {
                line = line.trim();
                if (line.isEmpty() || line.startsWith("#")) {
                    continue;
                }
                int eq = line.indexOf('=');
                settings.put(line.substring(0, eq), line.substring(eq + 1));
            }
        }
        System.out.println(settings);
        Files.delete(cfg);
        Files.delete(dir);
    }
}

How It Works

  1. Java 7 overload. The charset is passed explicitly, so this example also runs on Java 7.
  2. Ignore the noise. The comment line and the empty line are skipped with continue.
  3. Split once. indexOf('=') finds the first equals sign, so a value containing = would stay intact.
  4. Sorted printing. A TreeMap orders the keys, so host prints before port although port came first in the file.

Example 3 — Count Lines and Words with reader.lines()

BufferedReader.lines() (Java 8) turns the rest of the file into a stream.

Java
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;

public class NewBufferedReaderWords {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path text = dir.resolve("fox.txt");
        Files.write(text, "the quick brown fox\njumps over\nthe lazy dog".getBytes(StandardCharsets.UTF_8));
        List<String> lines;
        try (BufferedReader in = Files.newBufferedReader(text)) {
            lines = in.lines().collect(Collectors.toList());
        }
        int words = 0;
        for (String line : lines) {
            words += line.split("\\s+").length;
        }
        System.out.println("Lines: " + lines.size() + ", words: " + words);
        Files.delete(text);
        Files.delete(dir);
    }
}

How It Works

  1. Stream from a reader. in.lines() reads the remaining lines lazily; closing the reader also ends the stream.
  2. Split on whitespace. "\\s+" treats any run of spaces as one separator: 4, 2, and 3 words.
  3. Totals. Three lines and 4 + 2 + 3 = 9 words. A blank line would count as one empty "word" with this simple split, so real code should skip empty lines.

Frequently Asked Questions

Open try (BufferedReader in = Files.newBufferedReader(path)) and loop while ((line = in.readLine()) != null).
The one-argument overload (Java 8+) uses UTF-8. The Java 7 overload takes a Charset you choose.
The file contains bytes that are not valid in the charset. Open the reader with the file's real encoding, for example StandardCharsets.ISO_8859_1.
Yes. FileReader used the platform default charset before Java 11 and has fewer options; Files.newBufferedReader takes a Path and an explicit or UTF-8 charset.
Yes. Closing it closes the file. try-with-resources does it for you.

Next: Files.newBufferedWriter()

Open a file as a BufferedWriter for writing text efficiently.

newBufferedWriter() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful