1

System.in is a InputStream in Java.

The api for the read() method says

This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

 try {
   int inByte = System.in.read();
   System.out.println("Typed char: " + (char) inByte);
 } catch (Exception e) {
   e.printStackTrace();
 }

So now, I try to run this program above and it lets me type in many many characters and only stops blocking if I hit Enter on my keyboard.

Aren't there enough data available (how the api writes) when I only hit one key (that is one byte, e.g. A in UTF-8) and the program could continue afterwards?

The read(byte[]) behaves the same.. It blocks till the user presses Enter.

mrbela
  • 4,477
  • 9
  • 44
  • 79

0 Answers0