关于Java中read()函数的返回值的问题? [英] Issue regarding the return value of the read() function in java?

查看:52
本文介绍了关于Java中read()函数的返回值的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么InputStream#read()返回int并不是一个字节?

为什么read()的返回类型是整数?

嘿,我只是在Java文档上浏览I/O流的资料,而只是在参考字节流时才碰到这个问题-如果输入是字节流,为什么read()不返回字节值?

Hey I was just surfing java docs for material on I/O streams and just came across this question specifically in reference to the byte streams - If the input is a stream of bytes, why doesn't read() return a byte value ?

我挠了一下头,但是想不出任何有力的答案,如果有人能帮助我,我将不胜感激.

I scratched my head for some time but couldn't think of any strong answer to the question I would be grateful if anyone can help.

推荐答案

这是因为 read 可以返回-1作为流结束(EOF)标记的指示符-即不再有字节从流中读取.

This is because read can return -1 as an indicator of end-of-stream (EOF) marker - i.e. no more bytes to be read from the stream.

更多详细信息: http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#read%28%29 .具体来说:

More details: http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#read%28%29 . Specifically:

如果由于流的末尾已经没有字节而没有可用的字节到达时,返回值-1.

If no byte is available because the end of the stream has been reached, the value -1 is returned.

这篇关于关于Java中read()函数的返回值的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆