jjfjnu 发表于 2008-4-13 21:39

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

逐个字节去复制一个文件,效率超慢,都说用read(byte[]),查看api里面的似乎read(byte[])也是循环调用read函数代码如下
[code]    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;
[color=Red]        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }[/color]
        } catch (IOException ee) {
        }
        return i;
    }[/code]
不大清楚为什么read(byte[])也是逐个字节从磁盘读?????


页: [1]

编程论坛