输入流InputStream的read(byte b[], int off, int len)方法的使用
-
参数
byte b[] : the buffer into which the data is read. 读取数据的缓冲区
int off : the start offset in array byte b[] . byte b[]中的起始偏移量
int len: the maximum number of bytes to read.读取内容的最大长度
-
返回值
return: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. 读进缓冲区的bytes总数,如果没读到内容返回-1
-
关于子类
一些InputStream的实现类重写了read(byte b[], int off, int len)方法。
protected int pos; ByteArrayInputStream public synchronized int read(byte b[], int off, int len) { ... pos += len; return len; } BufferedInputStream private int read1(byte b[], int off, int len) throws IOException{ ... pos += cnt; return cnt; } ...以及其他子类Tips:这些子类在每次运行时,pos会改变大小,这样保证在这个实例中,每次读取能从前到后把这个实例的内容一次读取完。
本文详细解析了Java中输入流InputStream的read(byteb[],intoff,intlen)方法的使用,包括参数含义、返回值说明及子类如ByteArrayInputStream、BufferedInputStream等的重写实现,揭示了它们如何通过改变pos属性确保数据的连续读取。
742

被折叠的 条评论
为什么被折叠?



