new BufferedReader() / reader.readLine()
| Since: | Java 1.0(1996) |
|---|
BufferedReader is a class that buffers text input for efficient reading. It is commonly used to read files or standard input line by line. Combined with the try-with-resources syntax introduced in Java 7, it automatically handles closing the stream.
Syntax
// Create a BufferedReader to read from standard input.
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// Create a BufferedReader to read from a file.
BufferedReader reader = new BufferedReader(new FileReader("file path"));
// Read one line (returns null at the end of the file).
String line = reader.readLine();
Method List
| Class / Method | Description |
|---|---|
| new BufferedReader(Reader) | Wraps the specified Reader and adds buffering. |
| new InputStreamReader(InputStream) | Converts a byte stream to a character stream. You can also specify a character encoding. |
| readLine() | Reads one line as a string. The newline character at the end of the line is not included. Returns null when the end of the file is reached. |
| lines() | Returns all lines as a Stream<String> (Java 8+). Can be used together with stream operations. |
| close() | Closes the stream. Called automatically when using the try-with-resources syntax. |
Sample Code
BufferedReaderExample.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BufferedReaderExample {
public static void main(String[] args) {
// Read one line from standard input.
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
System.out.print("Enter input: ");
String line = reader.readLine();
System.out.println("You entered: " + line);
} catch (IOException e) {
e.printStackTrace();
}
// Read a file line by line.
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line); // Each line is printed.
}
} catch (IOException e) {
e.printStackTrace();
}
// Process lines as a stream using lines() (Java 8+).
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
br.lines()
.filter(l -> !l.isEmpty())
.forEach(System.out::println); // Each non-empty line is printed.
} catch (IOException e) {
e.printStackTrace();
}
}
}
The command looks like this:
javac BufferedReaderExample.java java BufferedReaderExample Enter input: Hello You entered: Hello Yagami Light L Lawliet Amane Misa Yagami Light Amane Misa
Common Mistakes
Common Mistake 1: Forgetting close() causes a resource leak
If you don't call close() on a BufferedReader after you're done with it, the file handle is not released and a resource leak occurs. It's like Yagami Light leaving evidence lying around instead of disposing of it.
ReaderLeakNg.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ReaderLeakNg {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("sample.txt"));
String line = br.readLine();
System.out.println(line);
}
}
The command looks like this:
javac ReaderLeakNg.java java ReaderLeakNg Yagami Light
Use the try-with-resources syntax to call close() automatically when the block exits. Just as L Lawliet carefully handles evidence, always close your resources reliably.
ReaderLeakOk.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ReaderLeakOk {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
String line = br.readLine();
System.out.println(line);
}
}
}
The command looks like this:
javac ReaderLeakOk.java java ReaderLeakOk Yagami Light
Common Mistake 2: Not checking for null from readLine() causes NullPointerException
readLine() returns null when it reaches the end of the file. If you forget the null check, calling a method on the returned value throws a NullPointerException. Check for null before proceeding, like Amane Misa double-checking her Shinigami Eyes deal.
ReadLineNullNg.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ReadLineNullNg {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
String line = br.readLine();
System.out.println(line.toUpperCase());
}
}
}
The command looks like this:
javac ReadLineNullNg.java java ReadLineNullNg Exception in thread "main" java.lang.NullPointerException
Always include a null check in the while condition when reading lines, as Near would cautiously verify every step before concluding.
ReadLineNullOk.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
class ReadLineNullOk {
public static void main(String[] args) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader("sample.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line.toUpperCase());
}
}
}
}
The command looks like this:
javac ReadLineNullOk.java java ReadLineNullOk YAGAMI LIGHT L LAWLIET AMANE MISA
Notes
Because BufferedReader uses an internal buffer, it is significantly faster than using FileReader alone, which reads one character at a time. readLine() returns null at the end of the file, so be sure to include a null check in your while condition.
Using the try-with-resources syntax from Java 7 onward (try (BufferedReader br = ...)) automatically calls close() when the block exits. Use this syntax to avoid accidentally forgetting to close the stream.
For reading an entire file at once, see Files.readString() / Files.readAllLines().
If you find any errors or copyright issues, please contact us.