ByteArrayOutputStream与ByteArrayInputStream说明

1、ByteArrayInputStream

ByteArrayInputStream 继承自 InputStream 

  • 包含一个内部缓冲区,其中包含可以从流中读取的字节。 内部计数器跟踪read方法要提供的下一个字节。

  • 关闭ByteArrayInputStream没有任何效果(调用close方法)。 在关闭流之后,可以调用此类中的方法,而不生成IOException 。

  • 使用和InputStream差不多一样,InputStream传入的是File路径,而ByteArrayInputStream传入的则是byte数组,使用如下。
ByteArrayInputStream inputStream = null;
String str = "测试ByteArrayInputStream";
byte[] bytes = str.getBytes();
inputStream = new ByteArrayInputStream(bytes);
byte[] bytes1 = new byte[1024];
int len;
while ((len = inputStream.read(bytes1)) != -1) {
    String s = new String(bytes1, 0, bytes1.length);
    System.out.println(s);
}
//close方法只是写了方法名,没有具体的关闭操作,不会抛出IOException
inputStream.close();

2、ByteArrayOutputStream

ByteArrayOutputStream 继承自outputStream

  • 实现了将数据写入字节数组的输出流。 当数据写入缓冲区时,缓冲区会自动增长。 数据可以使用toByteArray()toString() 。

  • 关闭ByteArrayOutputStream没有任何效果。 该流中的方法可以在流关闭后调用,而不生成IOException 。

常用方法
close() 关闭 ByteArrayOutputStream没有任何效果。 
size() 返回缓冲区的当前大小。
toByteArray() 创建一个新分配的字节数组。
toString() 使用平台的默认字符集将缓冲区内容转换为字符串解码字节。 
toString(String charsetName) 通过使用命名的charset解码字节,将缓冲区的内容转换为字符串。 
write(byte[] b, int off, int len) 从指定的字节数组写入 len字节,从偏移量为 off开始,输出到这个字节数组输出流。 
write(int b) 将指定的字节写入此字节数组输出流。 
writeTo(OutputStream out) 将此字节数组输出流的完整内容写入指定的输出流参数,就像使用 out.write(buf, 0, count)调用输出流的写入方法 out.write(buf, 0, count) 。 
reset() 将此字节数组输出流的 count字段重置为零,以便丢弃输出流中当前累积的所有输出。 

write(int b) 、write(byte[] b, int off, int len) 、 reset() 、size() 、toByteArray() 使用如下:

	public void test1() {
		byte[] bytes = "defghijk".getBytes();
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		outputStream.write(97);
		outputStream.write(98);
		outputStream.write(99);

		// 截取数组
		outputStream.write(bytes, 0, 5);
		// 获取数组长度
		int size = outputStream.size();
		// 获取当前截取后的数组
		byte[] toByteArray = outputStream.toByteArray();
		// 输出数组长度
		System.out.println(size);
		// 输出当前数组
		System.out.println(Arrays.toString(toByteArray));

		System.out.println("##########重置后");

		// reset 相当于清空数据
		outputStream.reset();
		// 输出数组长度
		System.out.println(outputStream.size());
		// 输出当前数组
		System.out.println(Arrays.toString(outputStream.toByteArray()));
	}

 toString(String charsetName) 、writeTo(OutputStream out) 、close()  

    public void test2() throws IOException {
        byte[] bytes="defghijk".getBytes("GBK");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        outputStream.write(bytes);
        //如果 编码与解码的格式不同意就会出现乱码现象
        String s = outputStream.toString("GBK");
        System.out.println(s);
        //和ByteArrayInputStream 的close方法一样
        outputStream.close();
        //将输出流的数据写入到另一个流中
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        outputStream.writeTo(out);
        //输出查看
        System.out.println(Arrays.toString(out.toByteArray()));
	}

3、ByteArrayOutputStream与ByteArrayInputStream联合使用:

 public static void main(String[] args) {
        try {
            read(write());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public static byte[] write() throws IOException {
        ByteArrayOutputStream outputStream=null;
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        String str = "测试ByteArrayOutputStream与ByteArrayInputStream";
        byte[] bytes = str.getBytes();
        stream.write(bytes);
        return stream.toByteArray();
 
    }
 
    public static void read(byte [] bytes) throws IOException {
        ByteArrayInputStream inputStream = null;
        inputStream = new ByteArrayInputStream(bytes);
        byte[] bytes1 = new byte[1024];
        int len;
        while ((len = inputStream.read(bytes1)) != -1) {
            String s = new String(bytes1, 0, bytes1.length);
            System.out.println(s);
        }
        //close方法只是写了方法名,没有具体的关闭操作,不会抛出IOException
        inputStream.close();
    }

原文地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值