在 java.io.FileWriter 中刷新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1742361/
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-12 22:08:47  来源:igfitidea点击:

flush in java.io.FileWriter

javastreamflushfilewriter

提问by i2ijeya

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:

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

So flush what it actually do?

那么flush它实际上做了什么?

采纳答案by Carl Smotricz

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

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

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.

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

回答by Ry4an Brase

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

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

回答by Benj

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

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

From the javadoc:

从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."

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

回答by Dave Costa

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.

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

回答by user15299

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.

为了回答您关于flush实际做什么的问题,它确保您写入流的任何内容(在您的情况下是文件)确实会在那里写入文件。

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.

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

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.

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

回答by Federico Giorgi

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

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

回答by Alexander Pogrebnyak

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

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

If you call flushexplicitly you may be sure that any IOExceptioncoming out of closeis really catastrophic and related to releasing system resources.

如果您flush明确调用,您可以确定任何IOException退出close都是灾难性的,并且与释放系统资源有关。

When you flushyourself, you can handle its IOExceptionin the same way as you handle your data write exceptions.

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

回答by Tom Hawtin - tackline

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

FileWriter是一个邪恶的类,因为它会选择碰巧在那里的任何字符集,而不是采用明确的字符集。即使您确实想要默认值,也要明确说明它。

The usual solution is OutputStreamWriterand 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.)

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

回答by Vojtech Salbaba

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