Monday, 6 June 2022

                                                                                                                        

 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 ClassCommand 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, shortfloatdouble, 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    

 

 

26 comments:

  1. Amazing write-up Team! The way you cover this topic in a single blog with excellent content is really appreciated. Keep it up.

    ReplyDelete
  2. Great work guys .. wish to see more content like these... Keep it up

    ReplyDelete
  3. This information is well understood in simple language & easy way.. Good job Guys 💯🤞

    ReplyDelete
  4. Enjoyed reading the blog above , really explains Boffer -Reader vs Scanner class in detail, the blog is very interesting and effective. Thank you and good luck in the upcoming blog.

    ReplyDelete
  5. gajab bhai yekdam lajavab blog banaya hai. Bohot information blog hai. I was looking for the same informative blog, and this blog ends my search. Thank you so much.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. very nice and very informative blog gyus keep it up and good going gyus

    ReplyDelete
  8. Explained to the point. Very helpful 👍

    ReplyDelete
  9. Very Useful information. Cleared the whole concept in just one read... Great work!!!

    ReplyDelete
  10. Keep it up guys, really productive blog !! 👍✌

    ReplyDelete
  11. Excellent job and very informative 👍👍🎉

    ReplyDelete
  12. Nice blog
    Very informative

    ReplyDelete
  13. Loved The Way You Have Written This Practical Blog With full Of knowledge and enthusiasm👍

    ReplyDelete
  14. Really an informative blog. Helped me understand the differences between Scanner class and Buffer Reader. Keep it up team.🙂

    ReplyDelete

                                                                                                                          Buffer-Reader Vs S...