java.io.FileWriter 中的刷新
我心中有一个问题,在写入文件时,在关闭完成之前,我们应该包含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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
正如其他人所说,在 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.
如前所述,您通常不需要冲洗。
仅当出于某种原因您希望另一个进程查看您正在使用的文件的完整内容而不关闭它时,这才有意义。例如,它可以用于由多个进程同时修改的文件,尽管需要非常小心:-)
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 :-)
FileWriter
是一个邪恶的类,因为它会选取恰好存在的任何字符集,而不是采用显式的字符集。即使您确实想要默认值,也要明确说明。通常的解决方案是
OutputStreamWriter
和FileOutputStream
。装饰器有可能抛出异常。因此,即使编写器从未构造过,您也需要能够关闭流。如果你打算这样做,你只需要刷新编写器(在愉快的情况下)并始终关闭流。 (只是为了混淆,一些装饰器,例如用于处理 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
andFileOutputStream
. 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.)程序中刷新的另一个用例是将长时间运行的作业的进度写入文件(以便可以停止并稍后重新启动)。您希望确保驱动器上的数据是安全的。
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.
写入器和流通常会在内存中缓冲一些输出数据,并尝试一次将其写入更大的块。刷新将导致立即从缓冲区写入磁盘,因此如果程序崩溃,数据不会丢失。当然,这并不能保证,因为磁盘可能不会立即物理写入数据,因此数据仍然可能会丢失。但这就不是 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.
我强烈建议在关闭之前调用flush。基本上它将剩余的缓冲数据写入文件。
如果您显式调用
flush
,您可能会确信任何来自close
的IOException
确实是灾难性的,并且与释放系统资源有关。当您自己
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 anyIOException
coming out ofclose
is really catastrophic and related to releasing system resources.When you
flush
yourself, you can handle itsIOException
in the same way as you handle your data write exceptions.您不需要进行刷新,因为 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."
为了回答您关于刷新实际上做了什么的问题,它确保您写入流的任何内容(在您的情况下是一个文件)确实会立即写入该文件。
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.
关闭自动冲洗。你不需要调用它。
Close automatically flushes. You don't need to call it.