【问题标题】:What happens internally when the outermost Stream is closed?当最外面的 Stream 关​​闭时,内部会发生什么?
【发布时间】:2014-12-10 11:57:42
【问题描述】:

我知道要关闭所有内部和封闭流,您可以关闭最外面的 Stream,它会处理封闭的 Stream,但它会等待在关闭内部流之前完全读取和写入数据,或者只执行所需的操作.

我只是想弄清楚当我们关闭封闭的 Stream 时会发生什么以及如何在内部流上调用内部函数?就像下面的代码一样

 public static void serialize(Object obj, String fileName)
        throws IOException {

    FileOutputStream fos = new FileOutputStream(fileName);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    bos.flush();
    oos.writeObject(obj);
    oos.close();
 }

当我关闭对象流时,它会等待文件流执行其功能吗?

【问题讨论】:

    标签: java


    【解决方案1】:

    是的,如果你关闭 ObjectOutputStream,所有挂起的写操作都会被执行。
    那是因为 ObjectOutputStream 调用了innerStream.flush()wenn 你调用了close() 就可以了。
    并且 BufferedOutputStream 也在自己的 flush 方法中调用了innerStream.flush()

    我知道的所有缓冲流类都这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多