六、字节数组流ByteArrayInputStream&ByteArrayOutputStream

一、前提

经常而言我们都是针对文件的操作,然后带上缓冲的节点流进行处理,但有时候为了提升效率,我们发现频繁的读写文件并不是太好,那么于是出现了字节数组流,即存放在内存中,因此有称之为内存流;

1.ByteArrayInputStream类

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

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

 1 import java.io.ByteArrayInputStream;
 2 import java.io.FileNotFoundException;
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 
 6 /**
 7  * 四个步骤:字节数组输入流
 8  * 1、创建源  : 字节数组 不要太大
 9  * 2、选择流
10  * 3、操作
11  * 4、释放资源: 可以不用处理
12  * 
13  * 14  *
15  */
16 public class IOTest07 {
17 
18     public static void main(String[] args) {
19         //1、创建源
20         byte[] src = "talk is cheap show me the code".getBytes();   //直接编码
21         //2、选择流
22         InputStream  is =null;
23         try {
24             is =new ByteArrayInputStream(src);
25             //3、操作 (分段读取)
26             byte[] flush = new byte[5]; //缓冲容器
27             int len = -1; //接收长度
28             while((len=is.read(flush))!=-1) {
29                 //字节数组-->字符串 (解码)
30                 String str = new String(flush,0,len);
31                 System.out.println(str);
32             }        
33         
34         } catch (IOException e) {
35             e.printStackTrace();
36         }finally {
37             //4、释放资源,可以不用写
38             try {
39                 if(null!=is) {
40                     is.close();
41                 }
42             } catch (IOException e) {
43                 e.printStackTrace();
44             }
45         }
46     }
47 
48 }

1.ByteArrayOutputStream类

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

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

     

     1 import java.io.ByteArrayOutputStream;
     2 import java.io.FileNotFoundException;
     3 import java.io.IOException;
     4 
     5 /**
     6  * 字节数组输出流 ByteArrayOutputStream
     7  *1、创建源  : 内部维护
     8  *2、选择流  : 不关联源
     9  *3、操作(写出内容)
    10  *4、释放资源 :可以不用
    11  *
    12  * 获取数据:  toByteArray()
    13  *  14  *
    15  */
    16 public class IOTest08 {
    17 
    18     public static void main(String[] args) {
    19         //1、创建源,可以不用创建,
    20         byte[] dest =null;
    21         //2、选择流 (新增方法)
    22         ByteArrayOutputStream baos =null;
    23         try {
    24             baos = new ByteArrayOutputStream();  //不需要指定路径
    25             //3、操作(写出)
    26             String msg ="show me the code";
    27             byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
    28             baos.write(datas,0,datas.length);
    29             baos.flush();
    30             //获取数据
    31             dest = baos.toByteArray();
    32             System.out.println(dest.length +"-->"+new String(dest,0,baos.size()));
    33         }catch(FileNotFoundException e) {        
    34             e.printStackTrace();
    35         }catch (IOException e) {
    36             e.printStackTrace();
    37         }finally{
    38             //4、释放资源
    39             try {
    40                 if (null != baos) {
    41                     baos.close();
    42                 } 
    43             } catch (Exception e) {
    44             }
    45         }
    46     }
    47 
    48 }

猜你喜欢

转载自www.cnblogs.com/qiaoxin11/p/12588876.html