Reading Console Input
In Java 1.9;the only way to perform ‘console input to use a byte stream, and older code that uses this approach persists. Today, using a yet stream to read console input· is still technically possible, but doing ad may require the use of a deprecated method, and this approach is not recommended. The preferred method of reading console input for java . is to update a character-oriented stream, which makes your program easier to internationalize and maintain.
In lava, console input is accomplished by reading from System.in. To obtain a crocheter-based stream that is attached to the console” you wrap System.in in a fluff: object, to create a character stream. Buffered Reader supports a Wilfred input stream. Its most commonly used con struct or is shown here: Here, input Reader is the stream that is linked to the instance of Buffered Arcadia that is, being created. Reader is an abstract class. One of its concrete sub classes is ‘ Input Stream Reader, which converts bytes to characters. To obtain an Input Stream Reader object that is linked to System in use the following constructor Input Stream Reader(Input Stream input Stream) Because System.in refers to an object of type Input Stream, it can be used for input Stream. Putting it all together, the following line of code creates a Differentiated that is connected to the keyboard:
To read a character from a Buffered Reader use ready ), The version of read( ) that we will be using is ‘int read( ) throws IO Exception Each time that read() is called reads a character from the input stream and returns it as an integer value. It returns -1 when the end of the stream is encountered. As you can see, it can throw an IO Exception. The following pro demonstrates ready ) by reading characters from the console until the user types.
This output may look a little different from what you expected, because System in is line buffered, by default. This means that no input is ‘actually passed to the program until you press ENTER. As you can guess, this does not make read() particularly valuable for interactive, console input.