Differences between Scanner and BufferReader Class in Java

DifferencesJavaObject Oriented ProgrammingProgramming

Scanner

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.Following are the important points about Scanner −

  • A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.

  • A scanning operation may block waiting for input.

  • A Scanner is not safe for multithreaded use without external synchronization.

BufferReader

The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter. This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them.

  • Instantiate an InputStreamReader class bypassing your InputStream object as a parameter.

  • Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter.

  • Now, read data from the current reader as String using the readLine() or read() method.

Differences

Following are the important differences between Scanner class and a BufferReader class.

Sr. No. Key Scanner Class BufferReader Class
1 Synchronous Scanner is not syncronous in nature and should be used only in single threaded case. BufferReader is syncronous in nature. During multithreading environment, BufferReader should be used.
2 Buffer Memory Scanner has little buffer of 1 KB char buffer. BufferReader 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. BufferReader is faster than Scanner as it only reads a character stream.
4 Methods Scanner has methods like nextInt(), nextShort() etc. BufferReader has methods like parseInt(), parseShort() etc.
5 Read Line Scanner has method nextLine() to read a line. BufferReader has method readLine() to read a line.

Example

Following example show how to read number using BufferedReader and Scanner −

JavaTester.java

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

Output

Enter an number:
1
You entered: 1
Enter an number:
2
You entered: 2

Example

Following example show how to read strings using BufferedReader and Scanner −

JavaTester.java

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class JavaTester { public static void main(String args[]) throws NumberFormatException, IOException { BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your name: "); String name = bufferReader.readLine(); System.out.printf("You entered: " + name); Scanner scanner = new Scanner(System.in); System.out.println("\nEnter your address:"); String address = scanner.nextLine(); System.out.printf("You entered: " + address); } }

Output

Enter your name: 
Mahesh
You entered: Mahesh
Enter your address:
India
You entered: India

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

raja
Updated on 19-Aug-2022 11:13:25

Advertisements