Java 流中的flush() 的用途是什么?
在Java中,flush()
方法用于流中。但我不明白使用这种方法的目的是什么?
fin.flush();
告诉我一些建议。
In Java, flush()
method is used in streams. But I don't understand what are all the purpose of using this method?
fin.flush();
tell me some suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
流通常由定期清空其内容的线程访问,例如,将其显示在屏幕上、将其发送到套接字或将其写入文件。这样做是出于性能原因。刷新输出流意味着您要停止,等待流的内容完全传输到其目的地,然后在流清空并发送内容的情况下恢复执行。
Streams are often accessed by threads that periodically empty their content and, for example, display it on the screen, send it to a socket or write it to a file. This is done for performance reasons. Flushing an output stream means that you want to stop, wait for the content of the stream to be completely transferred to its destination, and then resume execution with the stream empty and the content sent.
出于性能问题,首先将数据写入Buffer。当缓冲区已满时,数据将写入输出(文件、控制台等)。当缓冲区部分填充并且您想将其发送到输出(文件,控制台)时,您需要手动调用flush()方法,以便将部分填充的缓冲区写入输出(文件,控制台)。
For performance issue, first data is to be written into Buffer. When buffer get full then data is written to output (File,console etc.). When buffer is partially filled and you want to send it to output(file,console) then you need to call flush() method manually in order to write partially filled buffer to output(file,console).
来自文档
刷新
方法:缓冲主要是为了提高I/O性能。有关这方面的更多信息,请参阅这篇文章:调优 Java I/O 性能< /a>.
From the docs of the
flush
method:The buffering is mainly done to improve the I/O performance. More on this can be read from this article: Tuning Java I/O Performance.
当您将数据写入流时,数据不会立即写入,而是会被缓冲。因此,当您需要确保缓冲区中的所有数据均已写入时,请使用flush()。
我们需要确保在关闭流之前所有写入都已完成,这就是为什么在文件/缓冲写入器的
close()
中调用flush()
的原因。但是,如果您要求在关闭流之前随时保存所有写入,请使用
flush()
。When you write data to a stream, it is not written immediately, and it is buffered. So use
flush()
when you need to be sure that all your data from buffer is written.We need to be sure that all the writes are completed before we close the stream, and that is why
flush()
is called in file/buffered writer'sclose()
.But if you have a requirement that all your writes be saved anytime before you close the stream, use
flush()
.当我们发出任何命令时,该命令的流存储在我们计算机中称为缓冲区(临时内存位置)的内存位置中。当所有临时内存位置已满时,我们使用flush(),它刷新所有数据流并完全执行它们,并为缓冲区临时位置中的新流提供新空间。
-希望你能理解
When we give any command, the streams of that command are stored in the memory location called buffer(a temporary memory location) in our computer. When all the temporary memory location is full then we use flush(), which flushes all the streams of data and executes them completely and gives a new space to new streams in buffer temporary location.
-Hope you will understand
如果缓冲区已满,则缓冲在其上的所有字符串都将保存到磁盘上。缓冲区用于避免大交易!和开销。
在位于 java libs 中的 BufferedWriter 类中,有一行如下:
参考书: https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208
页数:453
If the buffer is full, all strings that is buffered on it, they will be saved onto the disk. Buffers is used for avoiding from Big Deals! and overhead.
In BufferedWriter class that is placed in java libs, there is a one line like:
reference book: https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208
pages:453
除了这里的其他很好的答案之外,这个解释对我来说非常清楚:
来源:https://www.youtube.com/watch?v=MjK3dZTc0Lg
In addition to other good answers here, this explanation made it very clear for me:
Source: https://www.youtube.com/watch?v=MjK3dZTc0Lg