Java BufferedReader vs Scanner Class
Java provides several classes for reading input, but two of the most commonly used are Scanner and BufferedReader. The main difference between Scanner and BufferedReader is:
- Scanner class provides parsing and input reading capabilities with built-in methods for different data types.
- BufferedReader class reads text efficiently from a character input stream.
Difference Between BufferedReader and Scanner Class
Aspect | Scanner | BufferedReader |
---|---|---|
Package | It is a part of java.util package. | It is a part of java.io package. |
Key use | Simple parsing of primitive types and strings | High-performance text reading |
Performance | Performance is slower due to parsing overhead and tokenization | Performance is faster due to efficient buffering |
Buffer Size | Buffer Size is smaller | Buffer Size is larger |
Thread-safe | It is not thread-safe. | It is thread-safe. |
Error Handling | Throws an exception like InputMismatchException | Throws an Exception like IOException |
Note: Both Scanner and BufferedReader can read from files.
Example:
new Scanner(new File("input.txt")) or,
new BufferedReader(new FileReader("input.txt"))
Scanner Class
We use the Scanner class when we need to read and parse input directly into primitive data types or strings, especially in small console-based applications or when performance is not a major concern.
Note: Scanner uses whitespace as the default delimiter. If users input multiple values on the same line (e.g., "Sweta 25"), Scanner will read them sequentially. You can change the delimiter using scanner.useDelimiter() if needed.
Example: The below Java program demonstrates the basic input and output operations using the Scanner class.
import java.util.Scanner;
public class Geeks {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = s.nextLine();
System.out.print("Enter your age: ");
int age = s.nextInt();
System.out.println("Name: " + name
+ ", Age: " + age);
s.close();
}
}
Output:

BufferedReader Class
We use BufferedReader when performance is important, especially for efficiently reading large volumes of data or files. BufferedReader reads large chunks of data at once, making it ideal for reading from files or processing large amounts of input.
Note: BufferedReader.readLine() throws a checked IOException, so it must be handled using a try-catch block or declared using throws IOException.
Example: The below Java program demonstrates reading user input from the console using BufferedReader.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Geeks {
public static void main(String[] args)
throws IOException
{
BufferedReader r = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter your name: ");
String name = r.readLine();
System.out.print("Enter your age: ");
int age = Integer.parseInt(r.readLine());
System.out.println("Name: " + name
+ ", Age: " + age);
}
}
Output:
