java.io.FileWriter 中的刷新

发布于 2024-08-11 06:34:06 字数 94 浏览 10 评论 0原文

我心中有一个问题,在写入文件时,在关闭完成之前,我们应该包含flush()吗??如果是这样,它到底会做什么?不流自动刷新? 编辑:

所以冲洗它实际上做了什么?

I have a question in my mind that, while writing into the file, before closing is done, should we include flush()??. If so what it will do exactly? dont streams auto flush??
EDIT:

So flush what it actually do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

对你而言 2024-08-18 06:34:07

正如其他人所说,在 close() 之前调用lush() 是没有意义的。使用flush() 的时机是当您保持文件打开状态但希望确保之前的写入已完全完成时。

There's no point in calling flush() just before a close(), as others have said. The time to use flush() is if you are keeping the file open but want to ensure that previous writes have been fully completed.

雨的味道风的声音 2024-08-18 06:34:07

如前所述,您通常不需要冲洗。

仅当出于某种原因您希望另一个进程查看您正在使用的文件的完整内容而不关闭它时,这才有意义。例如,它可以用于由多个进程同时修改的文件,尽管需要非常小心:-)

As said, you don't usually need to flush.

It only makes sense if, for some reason, you want another process to see the complete contents of a file you're working with, without closing it. For example, it could be used for a file that is concurrently modified by multiple processes, although with a LOT of care :-)

烟燃烟灭 2024-08-18 06:34:07

FileWriter 是一个邪恶的类,因为它会选取恰好存在的任何字符集,而不是采用显式的字符集。即使您确实想要默认值,也要明确说明。

通常的解决方案是 OutputStreamWriterFileOutputStream。装饰器有可能抛出异常。因此,即使编写器从未构造过,您也需要能够关闭流。如果你打算这样做,你只需要刷新编写器(在愉快的情况下)并始终关闭流。 (只是为了混淆,一些装饰器,例如用于处理 zip 的装饰器,具有确实需要关闭的资源。)

FileWriter is an evil class as it picks up whatever character set happens to be there, rather than taking an explicit charset. Even if you do want the default, be explicit about it.

The usual solution is OutputStreamWriter and FileOutputStream. It is possible for the decorator to throw an exception. Therefore you need to be able to close the stream even if the writer was never constructed. If you are going to do that, you only need to flush the writer (in the happy case) and always close the stream. (Just to be confusing, some decorators, for instance for handling zips, have resources that do require closing.)

妥活 2024-08-18 06:34:07

程序中刷新的另一个用例是将长时间运行的作业的进度写入文件(以便可以停止并稍后重新启动)。您希望确保驱动器上的数据是安全的。

while (true) {
  computeStuff();
  progresss += 1;
  out.write(String.format("%d", progress));
  out.flush();
}
out.close();

Another usecase for flushing in program is writing progress of longrunning job into file (so it can be stopped and restarted later. You want to be sure that data is safe on the drive.

while (true) {
  computeStuff();
  progresss += 1;
  out.write(String.format("%d", progress));
  out.flush();
}
out.close();
︶ ̄淡然 2024-08-18 06:34:06

写入器和流通常会在内存中缓冲一些输出数据,并尝试一次将其写入更大的块。刷新将导致立即从缓冲区写入磁盘,因此如果程序崩溃,数据不会丢失。当然,这并不能保证,因为磁盘可能不会立即物理写入数据,因此数据仍然可能会丢失。但这就不是 Java 程序的错了:)

当您写入行尾时,PrintWriters 自动刷新(默认情况下),当然,当您关闭流和缓冲区时,它们也会刷新。除此之外,只有当缓冲区已满时才会进行刷新。

Writers and streams usually buffer some of your output data in memory and try to write it in bigger blocks at a time. flushing will cause an immediate write to disk from the buffer, so if the program crashes that data won't be lost. Of course there's no guarantee, as the disk may not physically write the data immediately, so it could still be lost. But then it wouldn't be the Java program's fault :)

PrintWriters auto-flush (by default) when you write an end-of-line, and of course streams and buffers flush when you close them. Other than that, there's flushing only when the buffer is full.

你在我安 2024-08-18 06:34:06

我强烈建议在关闭之前调用flush。基本上它将剩余的缓冲数据写入文件。

如果您显式调用flush,您可能会确信任何来自closeIOException确实是灾难性的,并且与释放系统资源有关。

当您自己flush时,您可以像处理数据写入异常一样处理其IOException

I would highly recommend to call flush before close. Basically it writes remaining bufferized data into file.

If you call flush explicitly you may be sure that any IOException coming out of close is really catastrophic and related to releasing system resources.

When you flush yourself, you can handle its IOException in the same way as you handle your data write exceptions.

只为守护你 2024-08-18 06:34:06

您不需要进行刷新,因为 close() 会为您完成。

来自javadoc:

“关闭流,首先刷新它。一旦关闭流,进一步的 write() 或flush() 调用将导致抛出 IOException 。但是,关闭先前关闭的流没有任何效果。 ”

You don't need to do a flush because close() will do it for you.

From the javadoc:

"Close the stream, flushing it first. Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect."

空城仅有旧梦在 2024-08-18 06:34:06

为了回答您关于刷新实际上做了什么的问题,它确保您写入流的任何内容(在您的情况下是一个文件)确实会立即写入该文件。

Java可以进行缓冲,这意味着它会保留内存中写入的数据,直到达到一定数量,然后将其全部写入文件,这样效率更高。这样做的缺点是该文件不一定在任何给定时间都是最新的。 Flush 是“使文件保持最新状态”的一种说法。Close

首先调用lush,以确保关闭文件后包含您期望在其中看到的内容,因此正如其他人指出的那样,在关闭之前无需刷新。

To answer your question as to what flush actually does, it makes sure that anything you have written to the stream - a file in your case - does actually get written to the file there and then.

Java can perform buffering which means that it will hold onto data written in memory until it has a certain amount, and then write it all to the file in one go which is more efficient. The downside of this is that the file is not necessarily up-to-date at any given time. Flush is a way of saying "make the file up-to-date.

Close calls flush first to ensure that after closing the file has what you would expect to see in it, hence as others have pointed out, no need to flush before closing.

海的爱人是光 2024-08-18 06:34:06

关闭自动冲洗。你不需要调用它。

Close automatically flushes. You don't need to call it.

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