2

Hi guys while reading a stream of input data we use

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));

If BufferedReader is so good to read streams why cant we just use

BufferedReader stdin=new BufferedReader(System.in);

Why do we need InputStreamReader ??

Nathan Villaescusa
  • 17,331
  • 4
  • 53
  • 56
user1741642
  • 61
  • 1
  • 1
  • 4

4 Answers4

2

BufferedReader appears to be a wrapper class for the Reader class. Constructing a BufferedReader with System.in simply isn't valid syntax. The reason for the BufferedReader class comes from the documentation:

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,

BufferedReader in = new BufferedReader(new FileReader("foo.in"));

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.

1

Simply, because there is no BufferedReader(InputStream) constructor. Only, two constructors are available:

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

You can use BufferedInputStream(InputStream in) . I think this is what you are looking for...

MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
0

Input Stream Reader is connected to Buffered Reader through the PipeLining.

JVM will allocate some amount of buffer space to store the input at one shot and not by storing either byte by byte or character by character. So, after the input is completely entered by the user through the InputStreamReader, the input is stored in the buffer location allocated by JVM. With this Reader, the reading is faster for later processing.

Srinivas B
  • 1,821
  • 4
  • 17
  • 35