java这段代码中的:int length=in.read(b);,这个length接收到的是什么,read的值,还是数组b的值?

2025-03-25 12:16:30
推荐回答(5个)
回答1:

返回的是读取的长度,如果这个文件超过了你的byte数组,则返回byte数组的长度


回答2:

read();方法会将读取到的字节存入缓存数组b,并且return一个int类型的已经读取过字节的数组b的长度

如果读取失败那么返回-1;表示读取结束,或者一些异常。

至于为什么返回一个int类型而不是byte类型可详细阅读官方文档

网页链接

回答3:

in.read(b) 作用在於, 把资料读进来, 放在b.
in.read会返回一个int, 该return为读进资料的数量.

官方说明文件可以找到细节,
http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte[])

Parameters:
b - the buffer into which the data is read.

Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

回答4:

  1. read方法,参数是一个字节数组,这个字节数组,有本身初始化的长度,容量;

  2. read方法,返回的是每次读取文件内容的真实长度;

  3. 使用String类的构造方法,将真实长度传递进去,返回真实内容。

回答5:

应该是这个字节数组的长度吧/