Difference between Scanner and BufferReader in java

In this post, we will see difference between Scanner and BufferReader in java.

Java has two classes that have been used for reading files for a very long time.
These two classes are Scanner and BufferedReader.
[toc]
In this post, we are going to find major differences and similarities between the two classes.

Introduction

Let’s first go through quick introduction to Scanner and BufferReader classes.

Scanner

Scanner class can be used for user input by importing from java.util package.
A user can read numbers from System.in(From your command line) by using Scanner class.

Scanner sc  = new Scanner(System.in);
int i = sc.nextInt();

Let’s try a simple example to understand the working of the Scanner class.

package com.mycompany.hellonetbeans;

import java.util.Scanner;

/**
 *
 * @author arti
 */
public class UseOfScanner {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String name = scan.next();
        int num = scan.nextInt();
        scan.close();

        System.out.println("my name is: " + name);
        System.out.println("my lucky number is: " + num);

    }
}

The output of the above program will be:

FooBar 9
my name is: FooBar
my lucky number is: 9

If you don’t want to read from terminal only and want to read from a given file in that case, Scanner class can be used.

Scanner sc = new Scanner(new File("fileName"));
while (sc.hasNextLine()){
     System.out.println(sc.nextLine());
}

BufferedReader

BufferedReader class can be used by importing from java.io package. It reads characters from any input stream line. If you want to read from a file by using BufferedReader, you need to use an appropriate file reader. It can use readLine() to read data line by line.

BufferedReader br = new BufferedReader(new FileReader(“fileName”));

BufferedReader buffers characters and uses it in efficient readings of characters, arrays, lines, etc.

Let’s understand the functioning of BufferedReader by an example

package com.mycompany.hellonetbeans;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author arti
 */
public class BufferedReaderExample {

    public static void main(String[] args) throws IOException {

        String line;
        try {
            BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }

        } catch (FileNotFoundException ex) {
            Logger.getLogger(UseOfScanner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Output of the above code will be

Hi, this is an example.
line 1
line 2
line 3

Difference between Scanner and BufferedReader

  1. If you have a small application where you don’t care much about synchronization and concurrency, you can use the Scanner class whereas in a thread-safe environment BufferedReader will be a good choice.
  2. Scanner class has a smaller buffer memory of 1024 chars, whereas BufferedReader has a default buffer memory of 8192 chars, and that too can be extended.
  3. Scanner class comes from JDK 1.5, so old applications that support JDK 1.5 can not use Scanner class. BufferedReader class is with java from JDK 1.1, so it supports applications that use older versions of java.
  4. Scanner class uses regular expression in parsing the strings, by default, white space gets set as a delimiter, but you can set any other delimiter.
  5. BufferedReader is only used for reading data, whereas Scanner class is used for reading as well as parsing of data.

That’s all about difference between Scanner and BufferReader in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.