0

I want to know how the BufferedReader works? Why InputStreamReader is used with it? How it differs from the Scanner class which is also used to take user input? Which among the two is better?

Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42
  • 5
    http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html and http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html and http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html – EpicPandaForce Jun 30 '15 at 14:59
  • 2
    If you want to know; why don't you try to find out? Like, you yourself, by doing some prior research before coming here and asking other people to spent their time **teaching** you. – GhostCat Jun 30 '15 at 15:02
  • 1
    I'm voting to close this question as off-topic because it neither poses a specific programming question, nor demonstrates a basic level of effort. – MarsAtomic Jul 01 '15 at 00:58

2 Answers2

5

The main differences are

  1. 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.
  2. Another difference is the size of the buffer. Scanner has a much smaller buffer than the BufferedReader (1024 chars as opposed to 8192 chars). Although this may sound like quite a gap, the Scanner's buffer is more than enough for most tasks.
  3. Scanner also hides IOExceptions while BufferedReader throws them immediately, this presents advantages and disadvantages.
  4. Lastly, BufferedReader is synchronous while Scanner is not. Use BufferedReader if you're working with multiple threads.

You could have found this very easily by googling it.

Here's a good link.

Community
  • 1
  • 1
Olivier Poulin
  • 1,778
  • 8
  • 15
3

From javadoc of BufferedReader

Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

From javadoc of Scanner

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.

Basically BufferedReader reads efficiently chars.

Scanner split chars sequences in tokens (similar to words) and permit to access to some basic types(or classes) (like int, long, byte, double, String...). It is an helper class to parse input, not to buffer it.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56