Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader and InputStreamReader in Java

Tags:

java

c++

c

string

I recently started with Java and want to understand a java module of a large app. I came across this line of java code:

String line = (new BufferedReader(new InputStreamReader(System.in))).readLine();

What does this java code do. Is there a C/C++ equivalent of this?

like image 217
Shan Avatar asked Feb 06 '13 13:02

Shan


People also ask

What does InputStreamReader do in Java?

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset . The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

What is BufferedReader in Java?

Java BufferedReader is a public Java class that reads text, using buffering to enable large reads at a time for efficiency, storing what is not needed immediately in memory for later use. Buffered readers are preferable for more demanding tasks, such as file and streamed readers.

What is BufferedReader BufferedReader new BufferedReader new InputStreamReader system in ));?

BufferedReader input = new BufferedReader (new InputStreamReader (System.in)); Once we have created a BufferedReader we can use its method readLine() to read one line of characters at a time from the keyboard and store it as a String object. String inputString = input. readLine();

What is the difference between BufferedReader and FileReader in Java?

FileReader is used to read a file from a disk drive whereas BufferedReader is not bound to only reading files. It can be used to read data from any character stream.


1 Answers

System.in is the standard input.

InputStreamReader allows you to associate a stream that reads from the specified input (in this case the standard input), so now we have a stream.

BufferedReader is an "abstraction" to help you to work with streams. For example, it implements readLine instead of reading character by character until you find a '\n' to get the whole line. It just returns a String after this proccess.

So this line means: "Read a line from standard input and store it in line variable".

like image 150
arutaku Avatar answered Sep 22 '22 22:09

arutaku