为什么印刷作者的班级(和其他作家)在写作后需要打电话给冲洗?
我注意到Java中的某些I/O类(以及其他许多类,例如BufferedWriter和Filewriter),在写作后需要打电话给Flush()。 (除了AutoFlush,我稍后再进行)。
例如,这个呼叫println()将不起作用。但是,如果我调用作者#flush(),则该行将打印。
PrintWriter writer = new PrintWriter(System.out);
writer.println("test");
另外,是否以任何方式(尤其是在较大/一致的写入中)自动冲击影响性能,还是只是方便的,是否建议使用它?
I have noticed that some I/O Classes in Java (and a lot others, like the BufferedWriter and FileWriter), require a call to flush() after writing. (With the exception of AutoFlush, I'll get to that later).
For example, this call to println() will not work. However, if I invoke writer#flush() after, the line will print.
PrintWriter writer = new PrintWriter(System.out);
writer.println("test");
Also, does autoflushing impact performance in any way (especially in larger/consistent writes), or is it just a convenience, and is it recommended to use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
他们不需要冲洗,只有您要确保到目前为止显示输出,这正是Flushing。如果您写的是文件,并且只想在程序终止之前确保该文件到达那里,则无需冲洗。
They don't require flushing, only if you want to guarantee that output has been displayed so far, which is exactly what flushing is. If you are fine writing to a file and just want to make sure it gets there before the program terminates, then no need to flush.
从这里的书中阅读 - > ocp oracle oracle认证的专业java se 11
希望这对您来说很清楚!
Read from the book here -> OCP Oracle Certified Professional Java SE 11
Hope this is clear to you!
在想要冲洗 1 的范围内,如果打印作者下面的输出类“堆栈”正在进行一些输出缓冲,则需要。如果输出流被缓冲,则需要一些事件才能触发(冲洗)缓冲输出到外部文件,管道,套接字或其他任何事件。将触发冲洗的东西是:
collect()
上的东西的缓冲区,或flush()
。在
printwriter
的情况下,基础流也可以被类自动填充机制冲洗。缓冲输出的原因(通常)是效率。执行将数据写入(外部)文件,管道的低级输出操作,任何涉及系统调用的内容。这样做有很大的开销,因此您想避免做很多“小”写作。
输出缓冲是解决此问题的标准方法。要编写的数据收集在缓冲区中,直到缓冲区填充为止。网络结果我们可以将许多“小”写作汇总成“大”写作。性能的提高可能很重要。
避免明确冲洗确实是一种方便。它不会提高性能。实际上,如果您不需要 要冲洗的数据 1 ,那么不必要的自动填充将降低性能。
1-如果某人或某物想要尽快查看您正在编写的数据,您将希望数据泛滥。
To the extent that flushing is wanted1, it will be needed if the "stack" of output classes beneath the print writer is doing some output buffering. If an output stream is buffered, then some event needs to trigger pushing (flushing) the buffered output to the the external file, pipe, socket or whatever. The things that will trigger flushing are:
close()
on the stream, orflush()
on the stream.In the case of a
PrintWriter
, the underlying stream can also be flushed by the classes auto-flushing mechanism.The reason for buffering output (in general) is efficiency. Performing the low-level output operation that writes data to the (external) file, pipe, whatever involves a system call. There are significant overheads in doing this, so you want to avoid doing lots of "little" writes.
Output buffering is the standard way to solve this problem. Data to be written is collected in the buffer until the buffer fills up. The net result us lots of "little" writes can be aggregated into a "big" write. The performance improvement can be significant.
It is really a convenience to avoid having to explicitly flush. It doesn't improve performance. Indeed, if you don't need the data to be flushed1, then unnecessary auto-flushing will reduce performance.
1 - You would want the data to be flushed if someone or something wants to see the data you are writing as soon as possible.