Why did the method(read) stop when meet EOF?
You might be expecting it to read "learn!java" on the first call to read
, but that's not what read
is documented to do (emphasis mine):
The read
method of SequenceInputStream
tries to read the data from the current substream. If it fails to read any characters because the substream has reached the end of the stream, it calls the close
method of the current substream and begins reading from the next substream.
"Current substream" is the keyword here. It doesn't try to read data from any other substream, but the current one. Just after you created the SequenceInputStream
, the current substream is the first one - fin1
. The first call to read
will hence read from fin1
, and you won't get anything from fin2
.
The parameterless read
also says something similar.
This method tries to read one character from the current substream. If it reaches the end of the stream, it calls the close
method of the current substream and begins reading from the next substream.
How to read the next Stream?
Well, according to the second half of each quote, the SequenceInputStream
reads from the next stream when it can't read anything from the current stream anymore. In your code, the first call to finAll.read
read everything in fin1
, so the second call can't read anything from fin1
anymore, so it starts reading from fin2
.
If you want the data that is read to be in the same byte array, just change the offset parameter to the number of bytes read:
int bytesRead = finAll.read(box, 0, 10);
finAll.read(box, bytesRead, 10 - bytesRead);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…