为什么印刷作者的班级(和其他作家)在写作后需要打电话给冲洗?

发布于 2025-01-26 21:59:01 字数 314 浏览 2 评论 0原文

我注意到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 技术交流群。

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

发布评论

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

评论(3

予囚 2025-02-02 21:59:02

他们不需要冲洗,只有您要确保到目前为止显示输出,这正是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.

情深如许 2025-02-02 21:59:02

当数据写入输出流时,基础
操作系统不能保证数据能够实现
立即到文件系统。在许多操作系统中,
数据可能会在内存中缓存,仅发生写入
填充临时缓存或一段时间后
已经过去了。

如果数据缓存在内存中,并且应用程序终止
出乎意料的是,数据将丢失,因为它从来没有
写入文件系统。为了解决这个问题,所有输出流
课程提供了flush()方法,要求立即将所有累积数据写入磁盘。

flush()方法有助于减少丢失的数据量,如果
应用程序意外终止。这不是没有成本,
尽管。每次使用时,都可能导致明显的延迟
该应用程序,尤其是针对大文件。除非数据
您的写作非常关键,flush()方法应该
仅间歇性地使用。例如,它不应该
每条写后一定会被调用。


完成的写入数据,因为Close()方法将
自动执行此操作。

从这里的书中阅读 - > ocp oracle oracle认证的专业java se 11

希望这对您来说很清楚!

When data is written to an output stream, the underlying
an operating system does not guarantee that the data will make it
to the file system immediately. In many operating systems, the
data may be cached in memory, with a write occurring only
after a temporary cache is filled or after some amount of time
has passed.

If the data is cached in memory and the application terminates
unexpectedly, the data would be lost, because it was never
written to the file system. To address this, all output stream
classes provide a flush() method, which requests that all accumulated data be written immediately to disk.

The flush() method helps reduce the amount of data lost if the
application terminates unexpectedly. It is not without cost,
though. Each time it is used, it may cause a noticeable delay in
the application, especially for large files. Unless the data that
you are writing is extremely critical, the flush() method should
be used only intermittently. For example, it should not
necessarily be called after every write.

You also do not need to call the flush() method when you have
finished writing data, since the close() method will
automatically do this.

Read from the book here -> OCP Oracle Certified Professional Java SE 11

Hope this is clear to you!

前事休说 2025-02-02 21:59:01

为什么printwriter class(和其他作家)在写作后需要打电话给潮红?

在想要冲洗 1 的范围内,如果打印作者下面的输出类“堆栈”正在进行一些输出缓冲,则需要。如果输出流被缓冲,则需要一些事件才能触发(冲洗)缓冲输出到外部文件,管道,套接字或其他任何事件。将触发冲洗的东西是:

  • 流动填充
  • collect()上的东西的缓冲区,或
  • 流在流上调用flush()

printwriter的情况下,基础流也可以被类自动填充机制冲洗。

缓冲输出的原因(通常)是效率。执行将数据写入(外部)文件,管道的低级输出操作,任何涉及系统调用的内容。这样做有很大的开销,因此您想避免做很多“小”写作。

输出缓冲是解决此问题的标准方法。要编写的数据收集在缓冲区中,直到缓冲区填充为止。网络结果我们可以将许多“小”写作汇总成“大”写作。性能的提高可能很重要。


此外,是否以任何方式自动添加影响性能(尤其是在较大/一致的写入中),还是只是方便的,建议使用它吗?

避免明确冲洗确实是一种方便。它不会提高性能。实际上,如果您不需要 要冲洗的数据 1 ,那么不必要的自动填充将降低性能。


1-如果某人或某物想要尽快查看您正在编写的数据,您将希望数据泛滥。

Why does the PrintWriter class (and other writers) require a call to flush after writing?

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:

  • the buffer filling up
  • something calling close() on the stream, or
  • something calling flush() 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.


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?

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.

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