标题: IO流(read()方法返回值为什么是int? [打印本页] 作者: Sunshine_boy.. 时间: 2016-5-10 22:59 标题: IO流(read()方法返回值为什么是int? IO流(read()方法返回值为什么是int)? 作者: Sj改变 时间: 2016-5-11 00:42
API中的解释为:Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.
意思是每次从流中读取一比特数据。你可能会问为什么不返回byte数组。这就需要读源码了。
大概的原因就是。如果我们读去的流中有连续8个1,那么这个值在计算机内部表示-1;正好等于结束循环的标记,因此向上转型为int则避免了这个情况 的发生