As far I know, the two most common methods of reading character-based data from a file in Java is using Scanner
or BufferedReader
. I also know that the BufferedReader
read files efficiently by using a buffer to avoid physical disk operations. My questions are:
- Does
Scanner
performs as well asBufferedReader
? - Why would you choose
Scanner
overBufferedReader
or vice versa?
There are different ways of taking input in java like:
1) BufferedReader 2) Scanner 3) Command Line Arguments
BufferedReader Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
Where Scanner is a simple text scanner which can parse primitive types and strings using regular expressions.
if you are writing a simple log reader Buffered reader is adequate. if you are writing an XML parser Scanner is the more natural choice.
For more information please refer:
http://java.meritcampus.com/t/240/Bufferedreader?tc=mm69
Difference between BufferedReader and Scanner are following:
Code to read a line from console:
BufferedReader:
Scanner:
The Main Differences:
Example
prints the following output:
The same output can be generated with this code, which uses a regular expression to parse all four tokens at once:
BufferedReader:
Reads 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.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.
Source:Link
BufferedReader will probably give you better performance (because Scanner is based on InputStreamReader, look sources).ups, for reading from files it uses nio. When I tested nio performance against BufferedReader performance for big files nio shows a bit better performance.In currently latest JDK6 release/build (b27), the
Scanner
has a smaller buffer (1024 chars) as opposed to theBufferedReader
(8192 chars), but it's more than sufficient.As to the choice, use the
Scanner
if you want to parse the file, use theBufferedReader
if you want to read the file line by line. Also see the introductory text of their aforelinked API documentations.nextXxx()
methods inScanner
class.I prefer
Scanner
because it doesn't throw checked exceptions and therefore it's usage results in a more streamlined code.