Nil Nil - 4 months ago 9
Java Question

Bufferedreader explanation?

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


System.in(Standard input stream)-
gets the input from keyboard in bytes

InputStreamReader:
Converts the bytes into Unicode characters/ converts the standard input into reader object to be used with BufferedReader

Finally BufferedReader
: Used to read from character input stream(Input stream reader)

String c = br.ReadLine();
-- a method used to read characters from input stream and put them in the string in one go not byte by byte.

Is everything above right ? Please correct if anything wrong !

Answer

Nearly there, but this:

String c = br.readLine(); -- a method used to read characters from input stream and put them in the string in one go not byte by byte.

It reads characters from the input reader (BufferedReader doesn't know about streams) and returns a whole line in one go, not character by character. Think of it in layers, and "above" the InputStreamReader layer, the concept of "bytes" doesn't exist anymore.

Also, note that you can read blocks of characters with a Reader without reading a line: read(char[], int, int) - the point of readLine() is that it will do the line ending detection for you.

(As noted in comments, it's also readLine, not ReadLine :)