← Back to Core Java Course | Chapter 2: Input & Output | Lesson 4 of 6

Java BufferedReader

Introduction to BufferedReader

BufferedReader, from java.io, wraps another reader (commonly new InputStreamReader(System.in)) and buffers chunks of input internally, which cuts down on slow, repeated low-level read calls.

Example: Introduction to BufferedReader

java
import java.io.BufferedReader;
import java.io.StringReader;
public class Main {
	public static void main(String[] args) throws Exception {
		// new BufferedReader(new InputStreamReader(System.in)) is typical; StringReader stands in here
		BufferedReader br = new BufferedReader(new StringReader("buffered input"));
		System.out.println(br.readLine());
	}
}

Reading a Line of Text

readLine() pulls one full line of text at a time and returns it as a String, or returns null once there's nothing left to read -- checking for that null is how you detect end-of-input in a loop.

Example: Reading a Line of Text

java
import java.io.BufferedReader;
import java.io.StringReader;
public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new StringReader("first line"));
		String line = br.readLine();
		System.out.println(line != null ? line : "end of input");
	}
}

Reading Numbers

Since readLine() only ever gives you raw text, converting "42" into an actual int means explicitly calling Integer.parseInt() on the returned string yourself -- BufferedReader does no numeric parsing.

Example: Reading Numbers

java
import java.io.BufferedReader;
import java.io.StringReader;
public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new StringReader("42"));
		int number = Integer.parseInt(br.readLine()); // readLine() only gives raw text
		System.out.println(number + 1);
	}
}

Handling Exceptions

Nearly every BufferedReader method declares it can throw IOException, a checked exception, so your code won't even compile unless you wrap the calls in a try-catch or declare throws IOException on the enclosing method.

Example: Handling Exceptions

java
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;
public class Main {
	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new StringReader("data"));
		try {
			System.out.println(br.readLine());
		} catch (IOException e) { // readLine() throws a checked exception
			System.out.println("I/O error: " + e.getMessage());
		}
	}
}

BufferedReader vs Scanner

BufferedReader's larger internal buffer makes it noticeably faster for reading large files line by line, while Scanner's built-in type parsing (nextInt, nextDouble) makes it more convenient for small, interactive command-line programs.

Example: BufferedReader vs Scanner

java
import java.io.BufferedReader;
import java.io.StringReader;
public class Main {
	public static void main(String[] args) throws Exception {
		// BufferedReader: faster for large files, no built-in type parsing
		BufferedReader br = new BufferedReader(new StringReader("line by line"));
		System.out.println(br.readLine());
		// Scanner: slower, but nextInt()/nextDouble() parse types for you directly
	}
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.