Buffer-Reader Vs Scanner class in Java
As we know, Java is an object-oriented programming language; Hence everything is structured or wrapped into objects and classes. Unlike C, java does not have scanf() statements for taking the input from the user in the Command line (console). Nevertheless, Java has several input methods like Scanner class, BufferedReader Class, Command line Argument, and Console Class.
BufferedReader class is present in java.io package
while Scanner class is present in java.util package.
Even though both BufferedReader and Scanner can
read a file or user input from the command prompt in Java, there are some
significant differences between them. One of the main differences between
BufferedReader and Scanner class is that the former class
is meant to just read String or text data while the Scanner class is meant to
both read and parse text data into Java primitive types like int, short, float, double, and long. In other words, BufferedReader can
only read String but Scanner can read both String and other data types like int,
float, long, double, float, etc. This functional difference drives several
other differences in their usage.
Scanner is newer than BufferedReader, only introduced
in Java 5, while BufferedReader is present in Java from JDK 1.1 version.
This means, one has access to BufferedReader in almost all JDK
versions mainly Java 1.4 but Scanner is only available after Java 5.
BufferedReader has a significantly large buffer (8KB) than Scanner (1KB), which means if you are reading long String from a file, you should use BufferedReader but for short input and input other than String, you can use Scanner class.
Scanner uses regular expression to read and parse text input. It can accept custom delimiter and parse text into primitive data type e.g., int, long, short, float double using nextInt(), nextLong(),nextShort(), nextFloat(), nextDouble() method while BufferedReader can only read and store String using readLine() method.
Another major difference between BufferedReader and
Scanner class is that BufferedReader is synchronized while Scanner is not. This
means, you cannot share Scanner between multiple threads but you can share BufferedReader
object. This synchronization also makes the BufferedReader a little bit slower
in the single-thread environment as compared to Scanner, but the speed
difference is (regular expression), which eventually makes BufferedReader faster for
reading String.
Though both BufferedReader and Scanner can be used
to read a file, Scanner is usually used to read user
input and BufferedReader is commonly used to read a file
line by line in Java.
One reason for this is Scanner's ability to read
String, int, float, or any other data type and BufferedReader's
larger buffer size which can hold big lines from a file in memory.
Though it's not a restriction and
you can even read a file
using Scanner in Java. Alternatively, you can even read a file in just one line of
code in Java 8.
BufferedReader throws CheckedException (i.e IOException)
while Scanner does not throw any CheckedException.
Batch:-1 Group:-4
14)Yash Pathak
15)Sahil Pathan
19)Rakhi Pete
23)Aditya Rathod
34)Saket Dhake
35)Jitendra Sanap