Home java What is the difference between the return value available () and read...

What is the difference between the return value available () and read (byte [] b)

Author

Date

Category

Read from the File File File FileInputStream INPUT We obtain data values. Didn’t the Available method, should not return the number of available bits as well as the second design?

file contains the following text:

javatest1


Answer 1, Authority 100%

  • .available () does not block the execution thread when there is no data, and .read () blocks.
  • .available () Returns approximate number available for reading bytes, and .read (byte [] b) – the amount of read buffer (byte array), from -1 When the end of the data stream is reached and up to the size of the array (B.Length ).
  • .available () only checks the presence of data, and .read () – read them.

InputStream documentation .

The same is true for FileInputStream .


How does input.read (new byte [input.read ()]) :

  1. one byte is read from the stream using input.read () .
  2. its value (in this case, the code of the J 106 ), which returns .read () (it is the first byte value, and Not the amount of bytes), used to create an array of 106 bytes.
  3. In this byte array, data from flow is read.
  4. data in the stream remained 8 bytes (the first was already read in paragraph 1), and in the buffer they enter (106 & gt; 8 ), so .read (BYTE [] B ) Returns the amount of read bytes – 8.

Answer 2

The second way can not return the value greater than the size of the array.

p.s. To get the length of the file, use

new file ("Your File"). length ();

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions