BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.
Scanner is used for parsing tokens from the contents of the stream while BufferedReader just reads the stream and does not do any special parsing. In currently latest JDK6 release/build, the Scanner has a littler buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer), but it's more than enough.As to the choice, use the Scanner if you want to parse the file, use the BufferedReader if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.
A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReadercauses a corresponding read request to be made to underlying stream. Each invocation of read() or readLine() could cause bytes to be readfrom the file, converted into characters, and then returned, which can be very inefficient. Efficiency is improved appreciably if a Reader is warped in a BufferedReader. BufferedReader is synchronized, so read operations on a BufferedReadercan safely be done from multiple threads.
A scanner on the other hand has a lot more cheese built into it; it can do all that a BufferedReader can do and at the same level of efficiency as well. However, in addition a Scanner can parsethe underlying stream for primitive types and strings using regular expressions. It can also tokenize the underlying stream with the delimiter of your choice. It can also do forward scanning of the underlying stream disregardingthe delimiter! A scanner however is not thread safe, it has to be externally synchronized.
The choice of using a BufferedReader or a Scanner depends on the codeyou are writing, if you are writing a simple log reader Buffered readeris adequate. However if you are writing an XML parser Scanner isthe more natural choice.
THanks a Lot this is valuable information....
ReplyDeleteTHanks a Lot this is valuable information....
ReplyDeletethank u
ReplyDeletetnx sir share awsame tune.My Site
ReplyDeleteI need to to thank you for your time due to this fantastic read!!
ReplyDeleteWeb Designing Training In Bangalore
Web Design Courses In Bangalore
Java Training In Bangalore
iOS Training In Bangalore
UiPath Training in Bangalore by myTectra is one the best UiPath Training. myTectra is the market leader in providing Robotic Process Automation on UiPath
ReplyDeleteui path training in bangalore
I really loved the reading experience. Waiting for your future write-ups.
ReplyDeletePlacement Training in Chennai
Placement Training institutes
Placement Training Centres in Chennai
Unix Training in Chennai
Tally Institute in Chennai
Advanced Excel Training in Chennai
Hi,
ReplyDeleteBest article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take Best Training institutes for python course in BTM Layout