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
}
}
Call
What 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
Parameter
Description
path
The text file to open.
cs
(Java 7 overload) Charset used to decode.
Return Value
Return Value
Description
BufferedReader
An open reader positioned at the start of the file. Close it when done.
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);
}
}
Output
Run with java NewBufferedReaderNumbered. The output is the same on Windows, macOS, and Linux.
1: Roses are red
2: Violets are blue
3: Java is fun
How It Works
Read, test, repeat. The assignment inside the while condition reads a line and checks it in one expression.
null means the end. After "Java is fun" there is nothing left, so readLine() returns null and the loop stops.
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);
}
}
Output
Run with java NewBufferedReaderConfig. The output is the same on Windows, macOS, and Linux.
{host=localhost, port=8080}
How It Works
Java 7 overload. The charset is passed explicitly, so this example also runs on Java 7.
Ignore the noise. The comment line and the empty line are skipped with continue.
Split once.indexOf('=') finds the first equals sign, so a value containing = would stay intact.
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);
}
}
Output
Run with java NewBufferedReaderWords. The output is the same on Windows, macOS, and Linux.
Lines: 3, words: 9
How It Works
Stream from a reader.in.lines() reads the remaining lines lazily; closing the reader also ends the stream.
Split on whitespace."\\s+" treats any run of spaces as one separator: 4, 2, and 3 words.
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.