问题描述
我正在寻找理论分析。我的意思是,缓冲系统如何工作以及使用flush提供什么优势?如果可能,请举例说明。
I'm looking for a theoretical analysis. I mean, how does the buffer system works and what advantage does using flush provide? Kindly illustrate with an example, if possible.
推荐答案
当您写入文本文件时, BufferedWriter
不会立即将其写入磁盘。相反,它将数据保存在内存中的缓冲区中。
When you are writing to a text file, BufferedWriter
does not write it to disk immediately. Instead, it keeps the data in a buffer in memory.
这样做的好处是许多小写将进入缓冲区,然后数据将写入磁盘一气呵成,即。用一个大写,而不是许多小写,这将是低效的。
This has the advantage that many small writes will go into the buffer, and then the data will be written to disk in one go, ie. with one big write, instead of many small writes, which would be inefficient.
当缓冲区已满时, BufferedWriter
将自己写出数据,即。当缓冲区已满时,它将执行与调用 flush()
相同的事情。
When the buffer is full, BufferedWriter
will write the data out on it's own, ie. it will do the same thing as calling flush()
when the buffer is full.
所以你什么时候打电话 flush()
手动?
So when should you call flush()
manually ?
-
当您需要打开数据时磁盘现在。如果你有一个程序在写入磁盘的同时从磁盘上的文件中读取数据,你可能需要确保所有的数据应该在磁盘上实际那里。
When you need data to be on disk now. If you have a program which reads data from the file on disk at the same time it is written, you may want to ensure all of the data which is supposed to be on disk is actually there.
如果你是通过网络连接写的,你可能想要调用 flush()
以便数据立即通过网络发送。
If you are writing through a network connection, you may want to call flush()
so that the data gets sent through the network immediately.
通常它不是必须调用 flush()
。只需在完成后编写并调用 close()
,并且不需要 flush()
为 close()
为你冲洗缓冲区。
Usually it is not necessary to call flush()
. Just write and call close()
when finished, and no need for flush()
as close()
flushes the buffer for you.
这篇关于对于printwriter对象,flush的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!