Java BufferedReader
In this page:
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
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());
}
}
Login to try C/C++/Java/PHP code in the editor
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
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");
}
}
Login to try C/C++/Java/PHP code in the editor
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
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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
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());
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: