Java流中flush()的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2340106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:19:46  来源:igfitidea点击:

What is the purpose of flush() in Java streams?

javajava-streamflush

提问by Venkat

In Java, flush()method is used in streams. But I don't understand what are all the purpose of using this method?

在 Java 中,flush()方法用于流中。但我不明白使用这种方法的所有目的是什么?

fin.flush();

tell me some suggestions.

告诉我一些建议。

采纳答案by codaddict

From the docsof the flushmethod:

从该方法的文档flush

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.

缓冲主要是为了提高 I/O 性能。可以从这篇文章中阅读更多相关信息:调整 Java I/O 性能

回答by Silvio Donnini

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.

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

回答by Biman Tripathy

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.

当您将数据写入流时,它不会立即写入,而是被缓冲。因此,flush()当您需要确保写入缓冲区中的所有数据时使用。

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().

我们需要确保在关闭流之前完成所有写入,这就是为什么flush()在 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().

但是,如果您要求在关闭流之前随时保存所有写入,请使用flush().

回答by user3004437

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

当我们发出任何命令时,该命令的流存储在我们计算机中称为缓冲区(临时内存位置)的内存位置中。当所有临时内存位置已满时,我们使用flush(),它会刷新所有数据流并完全执行它们,并为缓冲区临时位置中的新流提供新空间。-希望你会明白

回答by vijaya kumar

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).

对于性能问题,首先要写入 Buffer 中的数据。当缓冲区已满时,数据将写入输出(文件、控制台等)。当缓冲区部分填充并且您想将其发送到 output(file,console) 时,您需要手动调用 flush() 方法以将部分填充的缓冲区写入 output(file,console)。

回答by Semih Okan Pehlivan

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:

在放置在java库中的BufferedWriter类中,有一行如下:

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!

如果您确实想在缓冲区满之前发送数据,您可以控制。只需冲洗它。对 writer.flush() 的调用说,“现在发送缓冲区中的任何内容!

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

参考书:https: //www.amazon.com/Head-First-Java-Kathy-Sierra/dp/0596009208

pages:453

页数:453