zoukankan      html  css  js  c++  java
  • ByteArrayOutputStream

    ByteArrayOutputStream用法

    字节数组流:
    ByteArrayOutputStream:    可以捕获内存缓冲区的数据,转换成字节数组。
    ByteArrayoutputStream bout=new ByteArrayOutputStream();
    bout.write(int a);  bout.write(int b);  bout.write(int c);
    byte[] buf=bout.toByteArray();//获取内存缓冲中的数据
    for(int i=0;i<=buf.length;i++)
    {
      System.out.println(buf);
    }
    bout.close();
    注:通过调用reset()方法可以重新定位。
    ByteArrayInputStream: 可以将字节数组转化为输入流
    ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
    int data=0;
    while( (b=bin.read())!=-1)
    {
      System.out.println(b);
    }
    bin.close();
    
    与DataOutputStream&DataInputStream联合使用:
    
    ByteArrayOutputStream bout=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(bout);
    String name="suntao";
    int age=19;
    dos.writeUTF(name);
    dos.writeInt(age);
    byte[] buf=bout.toByteArray();//获取内存缓冲区中的数据
    dos.close();
    bout.close();
    
    ByteArrayInputStream bin=new ByteArrayInputStream(byte[] buf);
    DataInputStream dis=new DataInputStream(bin);
    String name=dis.readUTF();//从字节数组中读取
    int age=dis.readInt();
    dis.close();
    bin.close();
    
    注:  DataInputStream&DataOutputStream还可以与FileInputStream&FileOutputStream
    联合使用。
    其中:
    DataInputStream&DataOutputStream关心如何将数据从高层次的形式转化成低层次的形式.
    FileInputStream&FileOutputStream关心如何操作存储单元以接受和产生数据。
  • 相关阅读:
    Centos 安装配置iscsi
    Centos 部署Keepalive高可用软件
    Centos 编译安装Haproxy
    Centos 安装 Moosefs文件系统
    docker 存储
    hadoop碰到的 一个问题
    使用curl命令操作elasticsearch
    kafka集群下线broker节点实践方法(broker topic 迁移)
    kafka 安装
    redis3.2.11 安装
  • 原文地址:https://www.cnblogs.com/gym2017/p/7280929.html
Copyright © 2011-2022 走看看