Scanner vs. BufferedReader

Mads Mobæk picture Mads Mobæk · Feb 9, 2010 · Viewed 260.1k times · Source

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

Answer

Chandra Sekar picture Chandra Sekar · Feb 9, 2010

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.