Hello guys, welcome to my blog. Today, we'll speak over or thus other interesting Java interview questions, BufferedReader vs Scanner. It's non entirely of import from interview signal of thought but likewise to piece of occupation efficiently amongst Java. Even though both BufferedReader together with Scanner tin give notice read a file or user input from the ascendence prompt inward Java, at that spot or thus pregnant differences betwixt them. One of the master copy departure betwixt BufferedReader together with Scanner shape is that onetime shape is meant to only read String or text information land Scanner shape is meant to both read together with parse text information into Java primitive types like int, short, float, double, together with long.
In other words, BufferedRedaer tin give notice entirely read String but Scanner tin give notice read both String together with other information types similar int, float, long, double, float etc. This functional departure drives several other differences inward their usage, which we'll run across inward this article.
Another departure is Scanner is newer than BufferedReader, entirely introduced inward Java 5, land BufferedReader is nowadays inward Java from JDK 1.1 version. This means, yous direct maintain access to BufferedReader inward almost all JDK versions mainly Java 1.4 but Scanner is entirely available afterward Java 5.
This is likewise a pop core Java inquiry from interviews. Since many developer lack Java IO skill, questions similar this examine their noesis well-nigh API together with how to exercise or thus practical task.
You volition non entirely larn well-nigh those key differences well-nigh BufferedReader together with Scanner inward this article but likewise well-nigh how to usage them inward a Java program. Btw, if yous are novel inward the Java evolution world, I advise yous to outset start amongst a comprehensive course of written report is like The Complete Java Masterclass on Udemy.
It's rattling affordable together with yous tin give notice acquire it nether $10 sometimes. The course of written report is actually prissy together with updated for latest Java version, together with I recommend to both beginners together with intermediate developers who desire to larn Java inward depth.
Here are the v key differences betwixt the Scanner together with BufferedReader shape of Java API:
1. H5N1 scanner is a much to a greater extent than powerful utility than BufferedReader. It tin give notice parse the user input together with read an int, short, byte, float, long together with double apart from String. On the other hand, BufferedReader tin give notice entirely read String inward Java.
2. BuffredReader has a significantly large buffer (8KB) than Scanner (1KB), which agency if yous are reading long String from a file, yous should usage BufferedReader but for curt input together with input other than String, yous tin give notice usage Scanner class.
3. BufferedReader is older than Scanner. It's nowadays inward Java from JDK 1.1 onward but Scanner is entirely introduced inward JDK 1.5 release.
4. Scanner uses regular expression to read together with parse text input. It tin give notice convey custom delimiter together with parse text into primitive information type e.g. int, long, short, float or double using nextInt(), nextLong(), nextShort(), nextFloat(), together with nextDouble() methods, land BufferedReader tin give notice entirely read together with shop String using readLine() method.
5. Another major departure betwixt BufferedReader together with Scanner shape is that BufferedReader is synchronized while Scanner is not. This means, yous cannot portion Scanner betwixt multiple threads but yous tin give notice portion the BufferedReader object.
This synchronization likewise makes BufferedReader trivial flake slower inward unmarried thread surround equally compared to Scanner, but the speed departure is compensated yesteryear Scanner's usage of regex, which eventually makes BufferedReader faster for reading String. You tin give notice farther check read user input together with BufferedReader is unremarkably used to read a file business yesteryear business inward Java.
One argue for this is Scanner's mightiness to read String, int, float or whatever other information type together with BufferedReader's larger buffer size which tin give notice concord large lines from a file inward memory.
Though it's non a restriction together with yous tin give notice fifty-fifty read a file using Scanner inward Java. Alternatively, yous tin give notice fifty-fifty read a file inward only i business of code inward Java 8.
If yous similar books, yous tin give notice likewise read Core Java Volume ii - Advanced Features by Cay S. Horstmann to larn to a greater extent than well-nigh Java IO fundamentals. It's i of the key areas inward meat Java programming which split an intermediate Java developer to an goodness one.
You tin give notice run across that Scanner is capable of reading both String together with numeric information from ascendence line. You tin give notice likewise run across how piece of cake it is to read a file business yesteryear business using BufferedReader.
Here is a summary of all the differences betwixt Scanner together with BufferedReader inward Java:
That's all well-nigh the departure betwixt Scanner together with BufferedReader shape inward Java. Even though both are capable of reading user input from the console, yous should usage Scanner if an input is non large together with yous likewise desire to read dissimilar types of input e.g. int, float together with String. Use BufferedReader is yous desire to read the text without parsing. Since it has a larger buffer, yous tin give notice likewise usage to read long String inward Java.
Further Learning
Complete Java Masterclass
solution)How to read an Excel file inward Java? (solution) How to read a CSV file inward Java? (example) How to create a file together with directory inward Java? (answer) How to read an XML file inward Java? (answer) How to append text to an existing file inward Java? (example) 5 Free Java 8 together with Java nine Courses for Programmers (courses) 5 Free Data Structure together with Algorithm Courses (courses) 5 Free courses to larn Spring Core together with Spring Boot for Java developers (courses) 10 tips to acquire a ameliorate Java developer (tips)
In other words, BufferedRedaer tin give notice entirely read String but Scanner tin give notice read both String together with other information types similar int, float, long, double, float etc. This functional departure drives several other differences inward their usage, which we'll run across inward this article.
Another departure is Scanner is newer than BufferedReader, entirely introduced inward Java 5, land BufferedReader is nowadays inward Java from JDK 1.1 version. This means, yous direct maintain access to BufferedReader inward almost all JDK versions mainly Java 1.4 but Scanner is entirely available afterward Java 5.
This is likewise a pop core Java inquiry from interviews. Since many developer lack Java IO skill, questions similar this examine their noesis well-nigh API together with how to exercise or thus practical task.
You volition non entirely larn well-nigh those key differences well-nigh BufferedReader together with Scanner inward this article but likewise well-nigh how to usage them inward a Java program. Btw, if yous are novel inward the Java evolution world, I advise yous to outset start amongst a comprehensive course of written report is like The Complete Java Masterclass on Udemy.
It's rattling affordable together with yous tin give notice acquire it nether $10 sometimes. The course of written report is actually prissy together with updated for latest Java version, together with I recommend to both beginners together with intermediate developers who desire to larn Java inward depth.
1. BufferedReader vs Scanner inward Java
Anyway, let's acquire dorsum to the topic.Here are the v key differences betwixt the Scanner together with BufferedReader shape of Java API:
1. H5N1 scanner is a much to a greater extent than powerful utility than BufferedReader. It tin give notice parse the user input together with read an int, short, byte, float, long together with double apart from String. On the other hand, BufferedReader tin give notice entirely read String inward Java.
2. BuffredReader has a significantly large buffer (8KB) than Scanner (1KB), which agency if yous are reading long String from a file, yous should usage BufferedReader but for curt input together with input other than String, yous tin give notice usage Scanner class.
3. BufferedReader is older than Scanner. It's nowadays inward Java from JDK 1.1 onward but Scanner is entirely introduced inward JDK 1.5 release.
4. Scanner uses regular expression to read together with parse text input. It tin give notice convey custom delimiter together with parse text into primitive information type e.g. int, long, short, float or double using nextInt(), nextLong(), nextShort(), nextFloat(), together with nextDouble() methods, land BufferedReader tin give notice entirely read together with shop String using readLine() method.
5. Another major departure betwixt BufferedReader together with Scanner shape is that BufferedReader is synchronized while Scanner is not. This means, yous cannot portion Scanner betwixt multiple threads but yous tin give notice portion the BufferedReader object.
This synchronization likewise makes BufferedReader trivial flake slower inward unmarried thread surround equally compared to Scanner, but the speed departure is compensated yesteryear Scanner's usage of regex, which eventually makes BufferedReader faster for reading String. You tin give notice farther check read user input together with BufferedReader is unremarkably used to read a file business yesteryear business inward Java.
One argue for this is Scanner's mightiness to read String, int, float or whatever other information type together with BufferedReader's larger buffer size which tin give notice concord large lines from a file inward memory.
Though it's non a restriction together with yous tin give notice fifty-fifty read a file using Scanner inward Java. Alternatively, yous tin give notice fifty-fifty read a file inward only i business of code inward Java 8.
If yous similar books, yous tin give notice likewise read Core Java Volume ii - Advanced Features by Cay S. Horstmann to larn to a greater extent than well-nigh Java IO fundamentals. It's i of the key areas inward meat Java programming which split an intermediate Java developer to an goodness one.
2. 1 Java Program to usage Scanner together with BufferedReader
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; /** * Java Program to demonstrate how to usage Scanner together with BufferedReader shape inward * Java. * * @author WINDOWS 8 * */ public class ScannerVsBufferedReader{ public static void main(String[] args) { // Using Scanner to read user input Scanner scnr = new Scanner(System.in); System.out.println("======================================="); System.out.println("You tin give notice usage Scanner to read user input"); System.out.println("======================================="); System.out.println("Please hold upwardly into a String"); String name = scnr.nextLine(); System.out.println("You direct maintain entered " + name); System.out.println("Please hold upwardly into an integer"); int historic stream = scnr.nextInt(); System.out.println("You direct maintain entered " + age); scnr.close(); // Using BufferedReader to read a file System.out.println("======================================="); System.out.println("You tin give notice usage BufferedReader to read a file"); System.out.println("======================================="); FileReader fileReader; try { fileReader = new FileReader("abc.txt"); BufferedReader buffReader = new BufferedReader(fileReader); System.out.println("File contains next lines"); String business = buffReader.readLine(); while (line != null) { System.out.println(line); business = buffReader.readLine(); } buffReader.close(); fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } Output ======================================= You tin give notice usage Scanner to read user input ======================================= Please enter a String James You direct maintain entered James Please enter an integer 32 You direct maintain entered 32 ======================================= You tin give notice usage BufferedReader to read a file ======================================= File contains next lines 1. Which is best SmartPhone in the market? a) iPhone 6S b) Samsung Milky Way Edge c) Something else
You tin give notice run across that Scanner is capable of reading both String together with numeric information from ascendence line. You tin give notice likewise run across how piece of cake it is to read a file business yesteryear business using BufferedReader.
Here is a summary of all the differences betwixt Scanner together with BufferedReader inward Java:
That's all well-nigh the departure betwixt Scanner together with BufferedReader shape inward Java. Even though both are capable of reading user input from the console, yous should usage Scanner if an input is non large together with yous likewise desire to read dissimilar types of input e.g. int, float together with String. Use BufferedReader is yous desire to read the text without parsing. Since it has a larger buffer, yous tin give notice likewise usage to read long String inward Java.
Further Learning
Complete Java Masterclass
solution)
Thanks for reading this tutorial thus far. If yous similar this tutorial together with thus delight portion amongst your friends together with colleagues. If yous direct maintain whatever questions or feedback together with thus delight drib a note. If yous desire to dice on inward behave upon follow me on twitter, my id is javinpaul.
No comments:
Post a Comment