IO流_内存操作流的概述和讲解

101 篇文章 0 订阅
85 篇文章 0 订阅
package cn.itcast_02;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/*
 * 内存操作流:用于处理临时存储信息的,程序结束,数据就从内存中消失
 * 字节数组:
 * 		ByteArrayInputStream
 * 		ByteArrayOutputStream
 * 字符数组:
 * 		CharArrayReader
 * 		CharArrayWriter
 * 字符串:
 * 		StringReader
 * 		StringWriter
 */
public class ByteArrayStreamDemo {
	public static void main(String[] args) throws IOException {
		// 写数据
		// ByteArrayOutputStream()
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		// 写数据
		for (int x = 0; x < 10; x++) {
			baos.write(("hello" + x).getBytes());
		}

		// 释放资源
		// 通过查看源码我们知道这里什么都没做,所以根本不需要close();
		// baos.close();

		// public byte[] toByteArray()
		byte[] bys = baos.toByteArray();

		// 读数据
		// ByteArrayInputStream()
		ByteArrayInputStream bais = new ByteArrayInputStream(bys);

		// 读数据
		int len = 0;
		while ((len = bais.read()) != -1) {
			System.out.print((char) len);
		}

		// 释放资源
		// bais.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值