用于操作字节数组的流对象。
ByteArrayInputStream :在构造的时候,需要接收数据源,。而且数据源是一个字节数组。
ByteArrayOutputStream: 在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。
这就是数据目的地。
因为这两个流对象都操作的数组,并没有使用系统资源。
所以,不用进行close关闭。
在流操作规律讲解时:
源设备,
键盘 System.in,硬盘 FileStream,内存 ArrayStream。
目的设备:
控制台 System.out,硬盘FileStream,内存 ArrayStream。
用流的读写思想来操作数据。
*构造函数:
*ByteArrayOutputStream()
创建一个新的 byte 数组输出流。
ByteArrayOutputStream(int size)
创建一个新的 byte 数组输出流,它具有指定大小的缓冲区容量(以字节为单位)。
ByteArrayInputStream(byte[] buf)
创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。
ByteArrayInputStream(byte[] buf, int offset, int length)
创建 ByteArrayInputStream,使用 buf 作为其缓冲区数组。
*/
public class ByteArrayStream {
public static void main(String[] args){
methed_1();
}
public static void methed_1(){
byte[] ch=new String("dfghjh").getBytes();//定义一个字节数组,(toCharArray字符串转字节数组)
ByteArrayInputStream bis=new ByteArrayInputStream(ch);
byte[] b1=new byte[1024];
ByteArrayOutputStream bos=new ByteArrayOutputStream();//定义一个字节数组输出流,并不一定需要定义源目的
int len_1=0;
byte[] b2=new byte[1024];
int len_2=0;
while((len_1=bis.read(b1,0,ch.length))!=-1){
bos.write(b1,0,b1.length);//把b1当作数据源,开始输出。。
}
System.out.println(new String(b1));
System.out.println(bos.toString()+b2.length);
}
}