您对read()这个方法的重载版本可能不是非常了解.
给您看看read()方法的几个定义:
当read()方法没有参数时,即循环是:while((b=in.read())!=-1).
此时的read()方法的定义是:
Reads a byte of data from this input stream. This method blocks if no input is yet available.
从输入流读取一个字节的数据,如果没有输入,则此方法结束.
返回值为:
the next byte of data, or -1 if the end of the file is reached.
输入流中的下一个字节.或者在读取到文件结尾(EOF)时返回-1.
那什么情况下b是读取到的字节的个数呢?这种形式:while((b=in.read(buf,0,buf.length))!=-1)
此时read()方法的重载函数定义为:
Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.
从该输入流读取最多len个字节到字节数组中,如果读取到的字节不是0个.则保存到缓冲区中直到刷新缓冲区.否则不读取字节返回0.
这个read(byte[]b,int off,int len)方法的返回值为:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the file has been reached.
返回读取到缓冲区的字节的总数,或者在到达文件结尾时返回-1.
综上所述,在read()方法无参数时返回读取到的一个字节,在参数为read(byte[]b,int off,int len)时返回读取到的字节数量,而不是您认为的b一直是得到读取的字节数量.