输入流InputStream的read(byte b[], int off, int len)方法的使用

输入流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会改变大小,这样保证在这个实例中,每次读取能从前到后把这个实例的内容一次读取完。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值