温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java _io_数据流

发布时间:2020-07-02 05:32:35 来源:网络 阅读:217 作者:wx5d21d5e6e5ab1 栏目:编程语言
  • 保存数据和数据类型,方便后期直接获取数据类型而不用强转
  • 使得字符串以外的类型也能写进字节数组里
  • DataInputStream(字节流)和DataOutputStream(字节流)
  • 读取必须和写入的顺序一致

    //写出到字节数组
    ByteArrayOutputStream os=new ByteArrayOutputStream();
    DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(os));
    
    //操作数据类型,加数据
    dos.writeUTF("哈哈哈");  //保存字符串
    dos.writeInt(18);       //保存int类型
    dos.writeBoolean(false); //保存布尔类型
    dos.writeChar('q');     //保存char类型
    dos.flush();  //强制刷新,使数据读取到内存里(字节数组)
    
    byte[] datas=os.toByteArray();  //获取写入的数据
    //读取,顺序与写出保持一致,顺序不对会报错
    DataInputStream dis=new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)) );
    String s=dis.readUTF();
    int age=dis.readInt();
    boolean flag=dis.readBoolean();
    char ch=dis.readChar();
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI