为了账号安全,请及时绑定邮箱和手机立即绑定

IO之BufferedOutputStream

IO之BufferedOutputStream

倚天杖 2019-02-28 15:27:20
当使用BufferedOutputStream为其做缓冲的时候,dataOutputStream对象的size为0,而不使用BufferedOutputStream做缓冲时,dataOutputStream值获取正常? String string = "数据类型"; double d = 2.3; long p = 10L; byte[] byteArray = null; ByteArrayOutputStream bout = new ByteArrayOutputStream(); **//DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(bout));** DataOutputStream dataOutputStream = new DataOutputStream(bout); try { dataOutputStream.writeUTF(string); dataOutputStream.writeLong(p); dataOutputStream.writeDouble(d); **System.out.println(dataOutputStream.size());** byteArray = bout.toByteArray(); dataOutputStream.flush(); dataOutputStream.close(); bout.close(); } catch (IOException e) { e.printStackTrace(); } 为什么这样可以: DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new ByteArrayInputStream(bs))); try { System.out.println(dataInputStream.readUTF()); System.out.println(dataInputStream.readLong()); System.out.println(dataInputStream.readDouble()); } catch (IOException e) { e.printStackTrace(); }
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

dataOutputStream.flush();在先,System.out.println(dataOutputStream.size());在后

查看完整回答
反对 回复 2019-03-01
  • 1 回答
  • 0 关注
  • 362 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信