勿以恶小而为之,勿以善小而不为--------------------------刘备
劝诸君,多行善事积福报,莫作恶
上一章简单介绍了字符流Reader和Writer(三),如果没有看过,请观看上一章
以前我们都是将内容写入到文件里面,从文件里面读取内容,都是对具体的文件进行操作,所用的是FileOutputStream和FileInputStream.
其实,我们还可以将内容写入到内存里面,从内存里面读取相应的内容, 以内存为中介,进行存储和读取。 但是,内存一断电就没有了,不能存储,但是速度快。 内存,用的是 ByteArray, 所以对应的流就是 ByteArrayOutputStream 和ByteArrayInputStream.
一 ByteArrayOutputStream
一.一 方法
一.一.一 构造方法
一.一.一.一 方法
| 方法 | 作用 |
|---|---|
| ByteArrayOutputStream?() | 创建内存输出流,默认大小为32 |
| ByteArrayOutputStream?(int size) | 创建内存输出流,自定义缓冲区的大小 |
一.一.一.二 演示
1 2 3 4 5 6 | @Test public void conTest(){ //默认是32 ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(32); } |
一.一.二 普通读写方法
| 方法 | 作用 |
|---|---|
| void close() | 关闭一个 ByteArrayOutputStream没有任何效果。 |
| void write?(int b) | 将单个字节 写入到内存中 |
| void write?(byte[] b, int off, int len) | 将字节数组写入到内存里面 |
| writeTo?(OutputStream out) | 将内存中的数组读取出来,放置到输出流里面, 即往输出流里面输入内存中的数据 |
| byte[] toByteArray?() | 将内存中的数据读取出来,然后封装成一个字节数组,返回该字节数组 |
| String toString?(String charsetName) | 将内存中的数据读取出来,封装成一个字符串,返回该字符串 |
一.二 演示内存输出流
一.二.一 输出到字节数组和字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @Test public void writer1Test() throws Exception{ ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(32); //内存 byte[] b="你好啊,我是两个蝴蝶飞".getBytes("utf-8"); //将内容写入到内存里面。 byteArrayOutputStream.write(b); //读出内容,封装成字节数组,可用于图片和视频 byte[] bytes= byteArrayOutputStream.toByteArray(); System.out.println("字节输出内容:"+new String(bytes,"UTF-8")); //关闭是没有效果的 byteArrayOutputStream.close(); //读出内容,封装成字符串 String content=byteArrayOutputStream.toString("UTF-8"); System.out.println("字符串输出内容:"+content); } |
运行,控制台打印输出:

一.二.二 输出到文件输出流
Hello3.txt 目前的内容为:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @Test public void writer2Test() throws Exception{ ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(32); byte[] b="你好啊,我是两个蝴蝶飞".getBytes("utf-8"); //写入到内存里面 byteArrayOutputStream.write(b); //定义文件 File file=new File("E:"+ File.separator+"ideaWork"+File.separator+"Java2"+File.separator+"fileSrc" +File.separator+"Hello3.txt"); //这个文件存在 OutputStream outputStream=new FileOutputStream(file,true); //从内存里面读取内容,放置到输出流 byteArrayOutputStream.writeTo(outputStream); outputStream.close(); byteArrayOutputStream.close(); } |
运行程序,查看 Hello3.txt 的内容

二. ByteArrayInputStream
二.一 方法
二.一.一 构造方法
二.一.一.一 方法
| 方法 | 作用 |
|---|---|
| ByteArrayInputStream?(byte[] buf) | 将字节数组放置到内存里面 |
| ByteArrayInputStream?(byte[] buf, int offset, int length) | 将字节数组的一部分放置到内存里面 |
二.一.一.二 演示
1 2 3 4 5 6 7 8 | @Test public void conTest() throws Exception{ byte[] bytes="两个蝴蝶飞".getBytes("UTF-8"); ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes); //读入一部分 ByteArrayInputStream byteArrayInputStream1=new ByteArrayInputStream(bytes,0,bytes.length); } |
二.一.二 读取和关闭方法
| 方法 | 作用 |
|---|---|
| void close() | 关闭一个 ByteArrayInputStream没有任何效果。 |
| int read?() | 从内存中读一个 |
| int read(byte[] b) | 从内存中读取数据,放置到 b 字节数组里面 |
| int read?(byte[] b, int off, int len) | 从内存中读取 len个到字节数组 |
二.二 演示内存输入流 ByteArrayInputStream
二.二.一 将字符串读取到内存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @Test public void readTest() throws Exception{ //定义输入内存的数据源 byte[] bytes="你好啊,我是两个蝴蝶飞".getBytes("UTF-8"); //写入到内存里面了。 ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes); //写进来了,接下来,该读出来了。 //读出来 byte[] b=new byte[1024]; int len=-1; while((len=byteArrayInputStream.read(b))!=-1){ System.out.println("读出数据:"+new String(b,0,len)); } //关闭 byteArrayInputStream.close(); } |
运行程序,控制台打印输出:

二.二.二 将输入流中的内容读取到内存里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | @Test public void read2Test() throws Exception{ File file=new File("E:"+ File.separator+"ideaWork"+File.separator+"Java2"+File.separator+"fileSrc" +File.separator+"Hello3.txt"); InputStream inputStream=new FileInputStream(file); byte[] inputB=new byte[1024]; int len=-1; ByteArrayInputStream byteArrayInputStream=null; while((len=inputStream.read(inputB))!=-1){ //写入到内存里面了,一次读取内容放置到 innputB 字节数组里面了。 byteArrayInputStream=new ByteArrayInputStream(inputB,0,len); //写进来了,接下来,该读出来了。 byte[] b=new byte[1024]; int lenTemp=-1; while((lenTemp=byteArrayInputStream.read(b))!=-1){ System.out.println("读出数据:"+new String(b,0,len)); } } //关闭 byteArrayInputStream.close(); inputStream.close(); } |
运行程序,控制台打印输出:

谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!