Difference Between Scanner and BufferedReader Class in Java



Scanner and BufferedReader classes are used to read input from an external system. Scanner is normally used when we know input is of type string or of primitive types, and BufferedReader is used to read text from character streams while buffering the characters for efficient reading of characters.

What is Scanner Class?

The Scanner class is included in the java.util package. It is mostly used when the data type of the input is already known. We commonly use it with data ty+pes like strings, integers, floats, and booleans. It has built-in methods like nextInt(), nextDouble(), and nextLine() that help to take inputs easily. Scanner is not thread-safe, which means it can cause problems in multi-threaded programs.

Example

The following code shows how to take input from a user using Scanner class:

import java.util.Scanner;
public class JavaTester {
   public static void main(String args[]) {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Enter a number:");
      int a = scanner.nextInt();
      System.out.printf("You entered: " + a);
   }
}

Output

The above program produces the following output:

Enter a number:
2
You entered: 2

What is BufferedReader Class?

The BufferedReader class is included in the java.io package. It uses a buffer to read characters efficiently. BufferedReader takes input as a string by default and should be manually converted into other data types if required. It is commonly used in situations where performance matters, such as reading a large text file. BufferedReader is thread-safe and can be used safely in multithreaded programs.

Example

The following code shows how to take input using BufferedReader class:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class JavaTester {
   public static void main(String args[]) throws NumberFormatException, IOException {
      BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Enter a number:");
      int a = Integer.parseInt(bufferReader.readLine());
      System.out.printf("You entered: " + a);
   }
}

Output

The program produces the following output:

Enter a number:
1
You entered: 1

Difference Between Scanner and BufferedReader

Following are the important differences between Scanner class and a BufferedReader class:

Sr. No. Key Scanner Class BufferedReader Class
1 Synchronous Scanner is not synchronous in nature and should be used only in single threaded case. BufferedReader is synchronous in nature. During multithreading environment, BufferedReader should be used.
2 Buffer Memory Scanner has little buffer of 1 KB char buffer. BufferedReader has large buffer of 8KB byte Buffer as compared to Scanner.
3 Processing Speed Scanner is bit slower as it need to parse data as well. BufferedReader is faster than Scanner as it only reads a character stream.
4 Methods Scanner has methods like nextInt(), nextShort() etc. BufferedReader has methods like parseInt(), parseShort() etc.
5 Read Line Scanner has method nextLine() to read a line. BufferedReader has method readLine() to read a line.
Updated on: 2025-04-16T15:51:50+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements