Scanner vs. BufferedReader

0 votes
asked Feb 9, 2010 by mads-mob%c3%a6k

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 as BufferedReader?
  • Why would you choose Scanner over BufferedReader or vice versa?

14 Answers

0 votes
answered Jan 9, 2010 by balusc

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.

0 votes
answered Jan 9, 2010 by roman
  1. 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.
  2. For reading from file try Apache Commons IO.
0 votes
answered Feb 9, 2010 by chandra-sekar-s

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.

0 votes
answered Jan 11, 2011 by jomoos

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.

0 votes
answered Jan 15, 2011 by evgeniy

I suggest to use BufferedReader for reading text. Scanner hides IOException while BufferedReader throws it immediately.

0 votes
answered Jan 1, 2014 by knu

The answer below is taken from Reading from Console: JAVA Scanner vs BufferedReader

When read an input from console, there are two options exists to achieve that. First using Scanner, another using BufferedReader. Both of them have different characteristics. It means differences how to use it.

Scanner treated given input as token. BufferedReader just read line by line given input as string. Scanner it self provide parsing capabilities just like nextInt(), nextFloat().

But, what is others differences between?

  • Scanner treated given input as token. BufferedReader as stream line/String
  • Scanner tokenized given input using regex. Using BufferedReader must write extra code
  • BufferedReader faster than Scanner *point no. 2
  • Scanner isn’t synchronized, BufferedReader synchronized

Scanner come with since JDK version 1.5 higher.

When should use Scanner, or Buffered Reader?

Look at the main differences between both of them, one using tokenized, others using stream line. When you need parsing capabilities, use Scanner instead. But, i am more comfortable with BufferedReader. When you need to read from a File, use BufferedReader, because it’s use buffer when read a file. Or you can use BufferedReader as input to Scanner.

0 votes
answered Jan 11, 2014 by sujith-ps
  1. BufferedReader has significantly larger buffer memory than Scanner. Use BufferedReader if you want to get long strings from a stream, and use Scanner if you want to parse specific type of token from a stream.

  2. Scanner can use tokenize using custom delimiter and parse the stream into primitive types of data, while BufferedReader can only read and store String.

  3. BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads.

  4. Scanner hides IOException while BufferedReader throws it immediately.

0 votes
answered Jan 27, 2014 by ursinusthestrong

The Scanner class is the complement of Formater class (used to convert binary data into formatted text). Scanner reads formatted input and converts it into its binary form. Although it has always been possible to read formatted input, it required more effort than most programmers would prefer. Because of the addition of Scanner, it is now easy to read all types of numeric values, strings and other types of data, whether it comes from a disk file, the keyboard, or another source. Scanner can be used to read input from the console, a file, a string, or any other source that implements the Readable interface or ReadableByteChannel. For example, you can use Scanner to read a number from the keyboard and assign its value to a variable.

BufferedReader, on the other hand, is a character stream I/O class. Character streams provide a convenient way for input and output in terms of characters (Unicode). BufferedReader is mostly used for taking input from the console, System.in. It takes an InputStreamReader object as an argument.

0 votes
answered Jan 16, 2015 by manisha-mulchandani

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

0 votes
answered Jan 6, 2016 by roottraveller

Listing few...

java.util.Scanner class is a simple text scanner which can parse primitive types and strings. It internally uses regular expressions to read different types.

Java.io.BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of sequence of characters

1) BufferedReader is synchronous while Scanner is not. BufferedReader should be used if we are working with multiple threads.

2)BufferedReader has significantly larger buffer memory than Scanner. The Scanner has a little buffer (1KB char buffer) as opposed to the BufferedReader (8KB byte buffer), but it’s more than enough.

3)BufferedReader is a bit faster as compared to Scanner because Scanner does parsing of input data and BufferedReader simply reads sequence of characters.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...