package com.tz.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 缓冲区会随着数据的不断写入而增长,存入数组,不涉及底层资源操作
* ByteArrayInputStream:在构造的时候,需要接收数据源,而且数据源是一个字节数组。
* ByteArrayOutputStream:在构造的时候,不用定义数据目的,该对象中已经内部封装了可变长度的字节数组,这就是数据目的地
* 因为这两个流对象都操作的数组,并没有使用系统资源,所以不用进行close关闭
* @author Administrator
* 流操作规律:
* 原设备 键盘,System.in硬盘,FileStream内存ArrayStream
* 目的设备 控制台 System.out 硬盘 fileStream,内存arraystream
*用流的读写思想来操作数组
*/
public class ByteArrayInputStreamDemo {
public static void main(String[] args) throws IOException {
//数据源
ByteArrayInputStream bis=new ByteArrayInputStream("".getBytes());
//数据目的
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int by=0;
while((by=bis.read())!=-1){
bos.write(by);
}
bos.writeTo(new OutputStream() {
@Override
public void write(int b) throws IOException {
}
});
}
}