当前位置: 代码迷 >> Java相关 >> read()和read(byte[])差别在哪里
  详细解决方案

read()和read(byte[])差别在哪里

热度:492   发布时间:2008-04-13 21:39:13.0
read()和read(byte[])差别在哪里
逐个字节去复制一个文件,效率超慢,都说用read(byte[]),查看api里面的似乎read(byte[])也是循环调用read函数代码如下
程序代码:
    public int read(byte b[], int off, int len) throws IOException {
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }

    int c = read();
    if (c == -1) {
        return -1;
    }
    b[off] = (byte)c;

    int i = 1;
    try {
        for (; i < len ; i++) {
        c = read();
        if (c == -1) {
            break;
        }
        b[off + i] = (byte)c;
        }

    } catch (IOException ee) {
    }
    return i;
    }

不大清楚为什么read(byte[])也是逐个字节从磁盘读?????
搜索更多相关的解决方案: byte  read  int  len  off  

----------------解决方案--------------------------------------------------------
read(byte[])会将读来的字节在参数数组中保存的.而read()是不会的.
----------------解决方案--------------------------------------------------------
查看java文档
----------------解决方案--------------------------------------------------------
  相关解决方案