i/o java advanced | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

i/o java advanced

I noticed that the read (abstract) method of inputStream has different implementations. in somewhere like System.in and Sockets if reaches to end it waits for new input and blocks Thread. but in file it returns -1 if reaches to end of file. explain to me. also explain that is" inputStream available method" reliable to detect that stream reaches to end? despite new input.

26th Jun 2021, 11:22 AM
Mehran
Mehran - avatar
1 Answer
+ 2
Some types of InputStream eg from keyboard do not have the end, but you can stop reading it. File has exact number of bytes, it is easy to say where is end of file. by keyboard input available() returns how many unreaded bytes are in buffer, not bytes to end of stream also it is possible to simulate end by pressing ctrl+d or ctrl+z then read() returns -1
26th Jun 2021, 10:19 PM
zemiak