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.
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.
|
1 2 3 4 |
Scanner sc = new Scanner(System.in); int i = sc.nextInt(); |
Let’s try a simple example to understand the working of the Scanner class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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:
|
1 2 3 4 5 |
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.
|
1 2 3 4 5 6 |
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
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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
|
1 2 3 4 5 6 |
Hi, this is an example. line 1 line 2 line 3 |
Difference between Scanner and BufferedReader
-
If you have a small application where you don’t care much about
synchronizationandconcurrency, you can use the Scanner class whereas in a thread-safe environmentBufferedReaderwill be a good choice. -
Scannerclass has a smaller buffer memory of 1024 chars, whereasBufferedReaderhas a default buffer memory of 8192 chars, and that too can be extended. Scannerclass comes fromJDK 1.5, so old applications that support JDK 1.5 can not use Scanner class.BufferedReaderclass is with java fromJDK 1.1, so it supports applications that use older versions of java.- 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.
-
BufferedReaderis 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.