DataInputStream 中的 三个read方法比较
int read()
Reads the next byte of data from this input stream.
int read(byte[] b)
See the general contract of the read method of DataInput.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes
注:read(),读取DataInputStream中的字节,每次读取一个字符,例如:<tip>hello</tip>,每read()一次,读取字符串中的一个字符,返回该字符的ASCII值(0---255)。
例如: dis = new DataInputStream( hc.openInputStream() );
int ch;
while( ( ch = dis.read() ) != -1 ) {
responseMessage.append( (char)ch );//responseMessage 是string。
}
ch是read()返回值,通过(char)ch 可以将ch还原成字符。
int read(byte[] b)
See the general contract of the read method of DataInput.
int read(byte[] b, int off, int len)
Reads up to len bytes of data from this input stream into an array of bytes
这两个方法的返回值则是将inputstream中的内容读取到byte[]中,返回的是读取的长度。