Scanner vs. BufferedReader
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
reads files efficiently by using a buffer to avoid physical disk operations.
My questions are:
- Does
Scanner
perform as well asBufferedReader
? - Why would you choose
Scanner
overBufferedReader
or vice versa?
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 fact you can pass a BufferedReader
to a scanner
as the source of characters to parse.
In currently latest JDK6 release/build (b27), the Scanner
has a smaller buffer (1024 chars) as opposed to the BufferedReader
(8192 chars), but it's more than sufficient.
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.
-
Parsing = interpreting the given input as tokens (parts). It's able to give back you specific parts directly as int, string, decimal, etc. See also all those
nextXxx()
methods inScanner
class. - Reading = dumb streaming. It keeps giving back you all characters, which you in turn have to manually inspect if you'd like to match or compose something useful. But if you don't need to do that anyway, then reading is sufficient.
See this link, following is quoted from there:
A BufferedReader is a simple class meant to efficiently read from the underling stream. Generally, each read request made of a Reader like a FileReader causes a corresponding read request to be made to underlying stream. 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. Efficiency is improved appreciably if a Reader is warped in a BufferedReader.
BufferedReader is synchronized, so read operations on a BufferedReader can 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 parse the 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 disregarding the 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 code you are writing, if you are writing a simple log reader Buffered reader is adequate. However if you are writing an XML parser Scanner is the more natural choice.
Even while reading the input, if want to accept user input line by line and say just add it to a file, a BufferedReader is good enough. On the other hand if you want to accept user input as a command with multiple options, and then intend to perform different operations based on the command and options specified, a Scanner will suit better.
BufferedReader
has significantly larger buffer memory than Scanner. UseBufferedReader
if you want to get long strings from a stream, and useScanner
if you want to parse specific type of token from a stream.Scanner
can use tokenize using custom delimiter and parse the stream into primitive types of data, whileBufferedReader
can only read and store String.BufferedReader
is synchronous whileScanner
is not. UseBufferedReader
if you're working with multiple threads.Scanner
hides IOException whileBufferedReader
throws it immediately.