为什么读取/写入单个字节/字符,返回值/输入值都是int型,为什么不用byte/charInputStream
abstract  int read() 
          从输入流中读取数据的下一个字节。 
从输入流中读取数据的下一个字节。返回 0 到 255 范围内的 int 字节值。如果因为已经到达流末尾而没有可用的字节,则返回值 -1。OutputStream
abstract  void write(int b) 
          将指定的字节写入此输出流。 
将指定的字节写入此输出流。write 的常规协定是:向输出流写入一个字节。要写入的字节是参数 b 的八个低位。b 的 24 个高位将被忽略。 Reader
int read() 
          读取单个字符。 
读取单个字符。
返回:
作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),如果已到达流的末尾,则返回 -1 Writer
void write(int c) 
          写入单个字符。 
写入单个字符。要写入的字符包含在给定整数值的 16 个低位中,16 高位被忽略。 
参数:
c - 指定要写入字符的 int。 -------------------------------------------------------------而多个字节/字符的读取/写入,都是用的byte/char数组
Inputstream:
 int read(byte[] b) 
          从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。 
 int read(byte[] b, int off, int len) 
          将输入流中最多 len 个数据字节读入 byte 数组。 OutputStream
 void write(byte[] b) 
          将 b.length 个字节从指定的 byte 数组写入此输出流。 
 void write(byte[] b, int off, int len) 
          将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。 
Reader:
int read(char[] cbuf) 
          将字符读入数组。 
abstract  int read(char[] cbuf, int off, int len) 
          将字符读入数组的某一部分。 Writer:
void write(char[] cbuf) 
          写入字符数组。 
abstract  void write(char[] cbuf, int off, int len) 
          写入字符数组的某一部分。 

解决方案 »

  1.   

    因为读到EOF的时候要返回-1,除此之外正常读到的byte范围在0-255,所以不用int的话就没法返回-1了
      

  2.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。
      

  3.   

    /**
         * Reads a byte of data from this input stream. This method blocks
         * if no input is yet available.
         *
         * @return     the next byte of data, or <code>-1</code> if the end of the
         *             file is reached.
         * @exception  IOException  if an I/O error occurs.
         */
    这个是截取的源码中的注释部分.这样可以看到.如果字节流中还有数据则返回下一个数据字节类型..如果到达文件末尾了.就返回-1,所以返回-1好像是大部分语言规定的文件末尾的标识吧...因为字节流每次都是读取一个或多个字节,而1Byte=8bit,8个比特位能够表示0-255范围的数据所以..返回int是最好的选择
      

  4.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。

    beichui 说得对,根本原因是,如果是用byte类型,尽管也可以返回-1,但是怎么判断是真正读取到的字符(转化为int就是-1),还是表示文件结尾(读取完毕)呢?
      

  5.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。

    /**
         * Reads the next byte of data from the input stream. The value byte is
         * returned as an <code>int</code> in the range <code>0</code> to
         * <code>255</code>. If no byte is available because the end of the stream
         * has been reached, the value <code>-1</code> is returned. This method
         * blocks until input data is available, the end of the stream is detected,
         * or an exception is thrown.
         *
         * <p> A subclass must provide an implementation of this method.
         *
         * @return     the next byte of data, or <code>-1</code> if the end of the
         *             stream is reached.
         * @exception  IOException  if an I/O error occurs.
         */
    .我感觉不是这样..刚才做了个实验表示每一个字符都占一个字节..虽然文件的读取是按照byte来读取的,但是它的返回值跟byte一点关系都没有..而所有字符的ACII码就是从0-255范围内的..同样汉字占两个字节..所以一个汉字解析出来也会变成2个ACII码..所以每返回一个文件中的字节数据都是整数.没有是负数的可能..但是又因为byte能够表示-128-127的范围所以返回类型不能是byte类型的..而如果返回char类型的又没有办法表示什么时候到达了文件的末尾..所以返回类型只能是int型..
      

  6.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。

    beichui 说得对,根本原因是,如果是用byte类型,尽管也可以返回-1,但是怎么判断是真正读取到的字符(转化为int就是-1),还是表示文件结尾(读取完毕)呢?
    文件里的“-1”,是俩个字符'-'和'1'
      

  7.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。

    /**
         * Reads the next byte of data from the input stream. The value byte is
         * returned as an <code>int</code> in the range <code>0</code> to
         * <code>255</code>. If no byte is available because the end of the stream
         * has been reached, the value <code>-1</code> is returned. This method
         * blocks until input data is available, the end of the stream is detected,
         * or an exception is thrown.
         *
         * <p> A subclass must provide an implementation of this method.
         *
         * @return     the next byte of data, or <code>-1</code> if the end of the
         *             stream is reached.
         * @exception  IOException  if an I/O error occurs.
         */
    .我感觉不是这样..刚才做了个实验表示每一个字符都占一个字节..虽然文件的读取是按照byte来读取的,但是它的返回值跟byte一点关系都没有..而所有字符的ACII码就是从0-255范围内的..同样汉字占两个字节..所以一个汉字解析出来也会变成2个ACII码..所以每返回一个文件中的字节数据都是整数.没有是负数的可能..但是又因为byte能够表示-128-127的范围所以返回类型不能是byte类型的..而如果返回char类型的又没有办法表示什么时候到达了文件的末尾..所以返回类型只能是int型..恩,有道理,应该是这个原因了。byte类型有符号,而字节数据是没符号的,把byte类型强转成int,最低的8位就不包括符号位了。不过汉字可不一定占2个字节,这个跟文件的编码集有关系。
    另外,不能用char,但是为什么不用short呢?short也能达到类似效果吧,这个没想明白。
      

  8.   


    byte类型也是带符号的,范围是-128到127,可以返回-1的。个人观点, 关键还是java的栈操作最低操作单位最小也必须是int,byte入栈的操作命令是bipush,会补位成int类型入栈,IO操作返回int可能效率更高。

    /**
         * Reads the next byte of data from the input stream. The value byte is
         * returned as an <code>int</code> in the range <code>0</code> to
         * <code>255</code>. If no byte is available because the end of the stream
         * has been reached, the value <code>-1</code> is returned. This method
         * blocks until input data is available, the end of the stream is detected,
         * or an exception is thrown.
         *
         * <p> A subclass must provide an implementation of this method.
         *
         * @return     the next byte of data, or <code>-1</code> if the end of the
         *             stream is reached.
         * @exception  IOException  if an I/O error occurs.
         */
    .我感觉不是这样..刚才做了个实验表示每一个字符都占一个字节..虽然文件的读取是按照byte来读取的,但是它的返回值跟byte一点关系都没有..而所有字符的ACII码就是从0-255范围内的..同样汉字占两个字节..所以一个汉字解析出来也会变成2个ACII码..所以每返回一个文件中的字节数据都是整数.没有是负数的可能..但是又因为byte能够表示-128-127的范围所以返回类型不能是byte类型的..而如果返回char类型的又没有办法表示什么时候到达了文件的末尾..所以返回类型只能是int型..恩,有道理,应该是这个原因了。byte类型有符号,而字节数据是没符号的,把byte类型强转成int,最低的8位就不包括符号位了。不过汉字可不一定占2个字节,这个跟文件的编码集有关系。
    另外,不能用char,但是为什么不用short呢?short也能达到类似效果吧,这个没想明白。
    恩..是是..可能我用的时记事本写的汉字.所以就占2个字节了..对short是可以..这个我也不太清楚..求有人指导