A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

IO流(read()方法返回值为什么是int)?

1 个回复

倒序浏览
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则避免了这个情况 的发生
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马