探究java IO之ByteArrayInputStream类

ByteArrayInputStream是使用字节数组作为源的输入流的一个实现。这个类有两个构造函数,每个构造函数都需要一个字节数组来提供数据源:

ByteArrayInputStream(byte[] buf)
ByteArrayInputStream(byte[] buf, int offset, int length)

在此,buf是输入源。第二个构造函数从字节数组的子集创建InputStream对象,这个数组子集从start指定的索引位置的字符开始,共length个字节。

close()方法 对ByteArrayInputStream对象没有效果。所以,不需要为ByteArrayInputStream对象调用close()方法,但是如果这么做的话,也不会产生错误。

见下面一个例子:

package o1;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamTest {
    public static void main(String[] args) {
        String str = "import java.io.ByteArrayInputStream;";
        try(ByteArrayInputStream b = new ByteArrayInputStream(str.getBytes(),7,29)){
            int n;
            while((n = b.read()) != -1){
                System.out.print((char)n);
            }
            System.out.println();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

ByteArrayInputStream实现了mark()reset()方法。然而,如果没有调用mark()方法,那么reset()方法会将流指针设置为流的开头——在这种情况下,也就是设置为传递给构造函数的字节数组的开头。见下面两个例子:

package o1;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamTest2 {
    public static void main(String[] args) {
        String str = "import java.io.ByteArrayInputStream;";
        try(ByteArrayInputStream b = new ByteArrayInputStream(str.getBytes())){
            int n,m = 0;
            while((n = b.read()) != -1){
                char c = (char)n;
                System.out.print(c);
                if(c == ' ') {
                    b.mark(m);
                    break;
                }
                m++;
            }
            System.out.println();
            b.reset();
            while((n = b.read()) != -1){
                char c = (char)n;
                System.out.print(c);
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

例2:

package o1;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamTest3 {
    public static void main(String[] args) {
        String str = "import java.io.ByteArrayInputStream;";
        try(ByteArrayInputStream b = new ByteArrayInputStream(str.getBytes())){
            int n,m =0;
            while((n = b.read()) != -1){
                char c = (char)n;
                System.out.print(c);
                if(m >= 6) {
                    //只有reset()方法调用,没有mark()方法调用
                    b.reset();
                    m = 0;
                    System.out.println();
                    break;
                }
                m++;
            }
            while((n = b.read()) != -1){
                char c = (char)n;
                System.out.print(c);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}


转载于:https://my.oschina.net/fhd/blog/366465

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值