Java 流中的flush() 的用途是什么?

发布于 08-23 09:20 字数 115 浏览 9 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

゛时过境迁2024-08-30 09:20:58

流通常由定期清空其内容的线程访问,例如,将其显示在屏幕上、将其发送到套接字或将其写入文件。这样做是出于性能原因。刷新输出流意味着您要停止,等待流的内容完全传输到其目的地,然后在流清空并发送内容的情况下恢复执行。

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.

心如荒岛2024-08-30 09:20:58

出于性能问题,首先将数据写入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).

挖个坑埋了你2024-08-30 09:20:57

来自文档 刷新方法:

刷新输出流并强制写出所有缓冲的输出字节。刷新的一般契约是,调用它表示如果先前写入的任何字节已通过输出流的实现进行缓冲,则这些字节应立即写入其预期目的地。

缓冲主要是为了提高I/O性能。有关这方面的更多信息,请参阅这篇文章:调优 Java I/O 性能< /a>.

From the docs of the flush method:

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

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.

岁月染过的梦2024-08-30 09:20:57

当您将数据写入流时,数据不会立即写入,而是会被缓冲。因此,当您需要确保缓冲区中的所有数据均已写入时,请使用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's close().

But if you have a requirement that all your writes be saved anytime before you close the stream, use flush().

怼怹恏2024-08-30 09:20:57

当我们发出任何命令时,该命令的流存储在我们计算机中称为缓冲区(临时内存位置)的内存位置中。当所有临时内存位置已满时,我们使用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

萌面超妹2024-08-30 09:20:57

如果缓冲区已满,则缓冲在其上的所有字符串都将保存到磁盘上。缓冲区用于避免大交易!和开销。

在位于 java libs 中的 BufferedWriter 类中,有一行如下:

private static int defaultCharBufferSize = 8192;

如果您确实想在缓冲区满之前发送数据,您确实有控制权。只需冲洗即可。调用 writer.flush() 会说:“立即发送缓冲区中的所有内容!

参考书: 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:

private static int defaultCharBufferSize = 8192;

If you do want to send data before the buffer is full, you do have control. Just Flush It. Calls to writer.flush() say, "send whatever's in the buffer, now!

reference book: https://www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

pages:453

萌梦深2024-08-30 09:20:57

除了这里的其他很好的答案之外,这个解释对我来说非常清楚:

缓冲区是内存中用于存储数据流的一部分
(人物)。这些字符有时只会被发送到
当缓冲区已满或满足一个输出设备(例如监视器)
一定数量的字符。如果您这样做,这可能会导致您的系统滞后
只需要发送到输出设备的几个字符。冲洗()
方法将立即将缓冲区的内容刷新到输出
流。

来源:https://www.youtube.com/watch?v=MjK3dZTc0Lg

In addition to other good answers here, this explanation made it very clear for me:

A buffer is a portion in memory that is used to store a stream of data
(characters). These characters sometimes will only get sent to an
output device (e.g. monitor) when the buffer is full or meets a
certain number of characters. This can cause your system to lag if you
just have a few characters to send to an output device. The flush()
method will immediately flush the contents of the buffer to the output
stream.

Source: https://www.youtube.com/watch?v=MjK3dZTc0Lg

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文