本文介绍了关闭I / O流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不关闭流时会发生什么不好的事情?

What are the bad things that can happen when I don't close the stream?

关闭操作会自动刷新吗?

Does the close operation automatically flush?

程序退出后所有流程都关闭了吗?

Are all the streams closed after the program exits?

提前致谢。

推荐答案

当你不关闭你的溪流时可能发生的坏事:

Bad things that can happen when you don't close your streams:


  • 你可以跑其他文件句柄

  • 您认为写入磁盘的数据可能仍在缓冲区中(仅限)

  • 文件可能仍会被其他文件锁定流程(取决于平台)

  • ...

  • you can run out of file handles
  • data that you think is written to disk may still be in the buffer (only)
  • files might still be locked for other processes (depends on the platform)
  • ...

是的,关闭操作总是刷新流。

Yes, close operation always flushes the stream.

操作系统知道的所有文件句柄都已关闭。这实际上意味着 FileOutputStream FileInputStream 以及 Socket 将被关闭。但是如果你在 BufferedOutputStream 中包装 FileOutputStream 那么 BufferedOutputStream 操作系统 关闭/刷新因此,写入 BufferedOutputStream 但尚未刷新到 FileOutputStream 的数据可能会丢失。

All file handles that the OS is aware of are closed. This means effectively that FileOutputStream, FileInputStream and the input/output of a Socket will be closed. But if you wrap a FileOutputStream in a BufferedOutputStream then that BufferedOutputStream will not be known to the OS and won't be closed/flushed on shutdown. So data written to the BufferedOutputStream but not yet flushed to the FileOutputStream can be lost.

这篇关于关闭I / O流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 06:37