BufferedReader and InputStreamReader in Java

I recently started with Java and want to understand the java module of a large application. 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 an equivalent to C / C ++?

+6
source share
5 answers

System.in - standard input.

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

BufferedReader is an abstraction that helps you work with streams. For example, it implements readLine instead of reading character by character until it finds '\ n' to get the whole line. It just returns a string after this process.

So this line means: "Read the line from standard input and save it in the line variable."

+9
source

> What does this java code do :

String line - your string object

new BufferedReader().readLine() is an instance of BufferedReader for reading text from a character input stream; and readline() is a method that it implements to read up to a newline.

new InputStreamReader() gives you an instance of InputStreamReader , which is the "bridge" between the standard in the byte stream and the character stream that BufferedReader wants.

System.in - standard input (byte stream)


> Is there a C/C++ equivalent of this
Well ... there is no language called C / C ++ ...;)
Therefore, I assume that you need an answer for each of them.

There are no β€œstrings” in C, you should use an array of characters, but you can read data into an array of characters from stdin with something like:

 char input[100]; ... scanf("%99[^\n]", input); 

or

 fgets (input, 100 , stdin) 

In C ++ you should use:

 using namespace std; string line; getline(cin, line); 
+7
source

Your snippet uses the BufferedReader associated with InputStreamReader to read aline from the standard input console and save it to a String string.

Bufferedreader

Read text from a character input stream by buffering characters to ensure that characters, arrays, and strings are read efficiently. You can specify a buffer size or use the default size. The default value is large enough for most purposes. In general, each read request made from Reader invokes a request for a corresponding read from a base character or byte stream. Therefore, it makes sense to wrap the BufferedReader around any Reader whose read () operations can be expensive, such as FileReaders and InputStreamReaders.

BufferedReader # ReadLine ()

Read a line of text. A line is considered to be completed by any of the lines ('\ n'), carriage return ('\ r') or carriage return, followed immediately by a line.

InputStreamReader

InputStreamReader is a bridge from byte streams to character streams: it reads bytes and decodes them into characters using the specified encoding. The encoding used may be specified by name or may be specified explicitly, or the default encoding of the platform may be adopted. Each call to one of the reading methods () of the InputStreamReader can result in reading one or more bytes from the underlying input / output stream. In order to efficiently convert bytes to characters, more bytes can be read ahead of the base stream than is necessary for the current read operation.

System

The System class contains several useful fields and methods of the class. It cannot be created.

The facilities provided by the System class include standard input, standard output, and error output streams; access to externally defined "properties"; file and library uploader; and a utility method for quickly copying part of an array.

System.in

"Standard" input stream. This stream is already open and ready for input. Typically, this stream corresponds to keyboard input or another input source specified by the host environment or user.

+4
source

What the code does is just read a line from the input stream. in terms of drawing, this is a decorator. Regarding the use of BufferedReader, it aims to improve I / O performance.

+1
source

InputStreamReader is a bridge from byte streams to character streams: it reads bytes and decodes them into characters using the specified encoding. The encoding used may be specified by name or may be specified explicitly, or the default encoding of the platform may be adopted.

Each call to one of the reading methods () of the InputStreamReader can result in reading one or more bytes from the underlying input / output stream. In order to efficiently convert bytes to characters, more bytes can be read ahead of the base stream than is necessary for the current read operation.

For maximum efficiency, consider wrapping the InputStreamReader in a BufferedReader. For instance:

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

+1
source

All Articles