Java BufferedReader vs Scanner Class
Last Updated :
28 May, 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:
- 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.
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 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.
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:
Similar Reads
BufferedReader Class lines() method in Java with Examples BufferedReader.lines() is the method of the Java Buffered Reader Class in the Java Library which returns lines in terms of Stream and from this Buffered Reader class. With the help of the stream, there are a lot of methods that mimic the output according to our needs. Syntax: BufferedReader.lines()
2 min read
BufferedReader close() method in Java with Examples The close() method of BufferedReader class in Java is used to close the stream and release all the system resources associated with the stream operations. Syntax: public void close() throws IOException Parameters: This method does not accept any parameter. Return value: This method does not return a
2 min read
Scanner Class in Java In Java, the Scanner class is present in the java.util package is used to obtain input for primitive types like int, double, etc., and strings. We can use this class to read input from a user or a file. In this article, we cover how to take different input values from the user using the Scanner clas
6 min read
BufferedReader mark() method in Java with Examples The mark() method of BufferedReader class in Java is used to mark the current position in the buffer reader stream. The reset() method of the same BufferedReader class is also called subsequently, after the mark() method is called. The reset() method fixes the position at the last marked position so
3 min read
Java User Input - Scanner Class The most common way to take user input in Java is using the Scanner class. It is a part of java.util package. The scanner class can handle input from different places, like as we are typing at the console, reading from a file, or working with data streams. This class was introduced in Java 5. Before
4 min read