8 Difference between BufferedReader and Scanner in Java With Example

Many java developers lack java I/O skill. You must go through at least the basic questions related to the java I/O which are frequently asked in the java developer interview. One of those questions is the difference between BufferedReader and Scanner in java. In this tutorial, I will share the difference between BufferedReader and Scanner and examples of BufferedReader and Scanner.

Read Also : How to Read Text File in Java


8 Difference between BufferedReader and Scanner in Java


1.Buffer Memory : BufferedReader has larger buffer memory(8KB or 8192 chars) than Scanner which has (1KB or 1024 chars). It means if you are reading a large String than you should use BufferedReader. If the user input is short or other than String, you can use Scanner.

2. Functionality : BufferedReader is used to read the data only. Scanner not only read the data but also parse the data.

Scanner uses regular expression to read and parse text. It can use custom delimiter and parse text into primitive data type for e,g short,long,int using nextShort(),nextLong(), nextInt() methods. BufferedReader can only read String using readLine() method.

3. Performance : BufferedReader is faster than Scanner because BufferedReader does not need to parse the data.

4. Data type : BufferedReader can read only String. Scanner can read String as well as primitive data types (int, float, double, long, short).

5. Introduction to JDK : BufferedReader was introduced in JDK 1.1 while Scanner was introduced in JDK1.5 .

6. Synchronization : BufferedReader is synchronized while Scanner is not. For multi-threading applications BufferedReader is preferred.

7. Checked Exception : BufferedReader throws CheckedException(i.e IOException) while Scanner does not throw any CheckedException.

8. Package : BufferedReader class is present in java.io package while Scanner class is present in java.util package.

Examples of BufferedReader and Scanner Class in Java



BufferedReader example taking a user input from console.


import java.io.*;

public class BufferedReaderExample {

    public static void main(String args[])

  {

    // Create an object of BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Enter blog name : ");
    
    //instantiate StringBuffer object
    StringBuffer sb = new StringBuffer("Hello "); 
 
    try{
          // read the input given by user        
          sb.append(br.readLine());
          // Close BufferedReader object
          br.close();
          // Showing Hello message
          System.out.println(sb);            

        } catch(IOException e) {
          e.printStackTrace();
        }
  }           

}             

Output : 
Enter blog name :
JavaHungry
Hello JavaHungry

Scanner example taking a user input from console.

import java.util.*;

public class ScannerExample {

    public static void main(String args[])

  {

    // Create an object of Scanner Class
    Scanner sc = new Scanner(System.in);

    System.out.println("Enter blog name : ");
    
    // read the input given by user   
    String name = sc.nextLine();

    //instantiate StringBuffer object
    StringBuffer sb = new StringBuffer("Hello ");

    sb.append(name);

    // Showing Hello message
    System.out.println(sb);

               
     System.out.println("Enter gender M/F  : ");
    
    // read the character input
    char gender = sc.next().charAt(0);

    System.out.println("Enter random integer  : ");

    // read the numerical input
    int randomInteger = sc.nextInt();

    System.out.println("Enter roll no.  : ");               

    // read long input
    long rollnumber = sc.nextLong();

    System.out.println(" Scanner is used to read user input");
    System.out.println(" gender : " + gender);
    System.out.println(" randomInteger : "+ randomInteger);
    System.out.println(" roll number : " + rollnumber);

    sc.close();
  }           

}

Output :
Enter blog name :
JavaHungry
Hello JavaHungry
Enter gender M/F  :
M
Enter random integer  :
10
Enter roll no.  :
01711015
Scanner is used to read user input
gender : M
randomInteger : 10
roll number : 01711015

As you can see in the Scanner example that it is able to read both String and numeric data from command line.


Recap : Difference Between BufferedReader and Scanner in Java


BufferedReaderScanner
Buffer MemoryLarge buffer(8KB) Small buffer(1KB)
FunctionalityRead data only Read and parse data
PerformanceFasterSlower
DataTyperead String onlyread String as well as primitive data type
Introduction to jdk Since jdk 1.1Since jdk 1.5
SynchronizationYesNo
CheckedExceptionThrows IOExceptionNo
Packagejava.iojava.util

In this article I have demonstrated the difference between BufferedReader and Scanner along with examples. You can use BufferedReader when the input is large and is data type of String. Use Scanner if the input is small or primitive data type (int, float, long, double). Please mention in comments in case you have any questions regarding BufferedReader or Scanner.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry