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 ()])
:
- one byte is read from the stream using
input.read ()
. - 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. - In this byte array, data from flow is read.
- 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 ();