FileReader流用read()方法读取文本后,为啥不能用int[]数组接收

FileReader#read()方法是Reader#read()接口方法的实现,所以答案就在Reader#read()方法的注释文档中:
* Reads a single character. This method will block until a character is* available, an I/O error occurs, or the end of the stream is reached.** \u0026lt;p\u0026gt; Subclasses that intend to support efficient single-character input* should override this method.** @return The character read, as an integer in the range 0 to 65535* (\u0026lt;tt\u0026gt;0x00-0xffff\u0026lt;/tt\u0026gt;), or -1 if the end of the stream has* been reached 【FileReader流用read()方法读取文本后,为啥不能用int[]数组接收】 java中char类型范围对应于16位非负整数,也就是0~65535,所以Reader#read()方法返回值应当是单个char类型字符( @小五 的解释欠妥)。
那为什么不直接用char类型呢?因为这里可能需要返回-1(EOF),char类型的整数范围不含负数,所以只能用int类型。

■网友
首先来看一下 read 方法在 JDK 中的源码:
/** * Reads a single character. * * @return The character read, or -1 if the end of the stream has been * reached * * @exception IOException If an I/O error occurs */ public int read() throws IOException { return sd.read(); }可以看到,当读到 EOF 的时候,read 方法会返回 -1,这就要求返回值类型的取值范围能够容纳 -1 这个值,char 的范围是 0 ~ 65535,int 的范围则是 -2147483648 ~ 2147483647。所以这个方法就选择了 int 作为返回值。而使用 char 来读取时显然不会遇到读 EOF 的问题,毕竟 read 方法还返回了一个 int 来表示读了多少字符。
至于为什么不用 int,没用,太占地方。

■网友
因为char能表示字节吧。
■网友
FileReader对象的read()方法返回的是int类型,为什么呢?为了存储该字符的UTF-16编码值。首先,Java中int占4个字节,而char占2个字节。准确得说,该方法读入的是一个码点,char叫码元。因为在UTF-16中,一个码点占1或两个码元,最多占4字节,所以要返回int。
■网友
为什么说char是int咧...这样那就只有3种基本类型了,boolean long double ...


    推荐阅读