什么时候使用flush()?

当程序输出数据到文件或者套接字的时候,会用到flush方法。

这个方法有什么用?
You use flush() to clear the internal buffers(if any) and force write(i.e. flush) all the pending data in to the stream destination.

什么时候使用这个方法呢?
If another process (or thread) needs to examine the file while it's being written to, and it's important that the other process sees all the recent writes.
If the writing process might crash, and it's important that no writes to the file get lost.
If you're writing to the console, and need to make sure that every message is shown as soon as it's written.
以上,在程序正常退出或者异常退出之前,或者让其他进程及时看到,或者工程师想立即看到输出结果。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容