Java:为什么InputStream的read()方法返回值是int而不是byte?

Ref: https://stackoverflow.com/questions/4659659/why-does-inputstreamread-return-an-int-and-not-a-byte

Because a byte can only hold -128 until 127, while it should return 0 until 255 (and -1 when there’s no byte left (i.e. EOF)). Even if it returned byte, there would be no room to represent EOF.
因为一个字节的范围是-128到127, 然而它应该返回0~255(当没有字节剩下时返回-1)。即使使用byte来作为返回值,那么就没有值来表示读取到了文件的末尾了。

发布了40 篇原创文章 · 获赞 1 · 访问量 1102

猜你喜欢

转载自blog.csdn.net/weixin_44495162/article/details/103434130