当程序输出数据到文件或者套接字的时候,会用到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.
以上,在程序正常退出或者异常退出之前,或者让其他进程及时看到,或者工程师想立即看到输出结果。