Last Updated : 23 Jul, 2025
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:
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:
Scanner Classnew Scanner(new File("input.txt")) or,
new BufferedReader(new FileReader("input.txt"))
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.
Java
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 ClassWe 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.
Java
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:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4