日期:2014-05-20  浏览次数:20880 次

java.io.InputStream的read()和read(byte[] b)和read(byte[] b, int off, int len)有什么不同
java.io.InputStream的read()和read(byte[] b)和read(byte[] b, int off, int len)有什么不同啊?什么时候用read()?什么时候用read(byte[] b),什么时候用read(byte[] b, int off, int len)啊?

同问java.io.OutputStream的write(byte[] b)和write(byte[] b, int off, int len) 和te(int b)方法!

------解决方案--------------------
read() 
Reads the next byte of data from the input stream. 
int read(byte[] b) 
Reads some number of bytes from the input stream and stores them into the buffer array b. 
int read(byte[] b, int off, int len) 
Reads up to len bytes of data from the input stream into an array of bytes. 

------解决方案--------------------
lz去查API最清楚,同一个方法,参数不同,根据需要调用了。
------解决方案--------------------
源码
Java code

public int read(byte b[]) throws IOException {
    return read(b, 0, b.length);
    }

------解决方案--------------------
read()读取1个字节
read(byte[] b)将文本中的所有数据读取到b这个字节数组中
read(byte[] b, int off, int len)将文本中的所有数据读入到b字节数组的指定位置(这个貌似是这样
的,没有去测试)

如楼上所说,这些都是可以在帮助文档里面查到的~
------解决方案--------------------
探讨

read()
Reads the next byte of data from the input stream.
int read(byte[] b)
Reads some number of bytes from the input stream and stores them into the buffer array b.
int read(byte[] ……

------解决方案--------------------
Java code

//从源码可以看出来,他们本来就是同一个东西
public int read(byte b[]) throws IOException {
    return read(b, 0, b.length);
}

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;
}
public abstract int read() throws IOException;

------解决方案--------------------
read()读取1个字节
read(byte[] b)将文本中的所有数据读取到b这个字节数组中
read(byte[] b, int off, int len)从流的第off个字节开始,读入长度为len的字节的数据



--signature-------------------
http://www.mowker.com/view/
------解决方案--------------------
API的干活