简体   繁体   English

用Java高效地连续写入文件

[英]Continuously write to file in Java efficiently

I have a program that's generating a bunch of output I want to to save to a text file.我有一个程序正在生成一堆我想保存到文本文件的输出。 I don't necessarily know when the program is going to end, and I want to have as little impact on performance as possible.我不一定知道程序什么时候结束,我希望对性能的影响尽可能小。 Right now I am buffering my lines into a Deque , then dumping that entire Deque into the file every 100 lines (number was arbitrary).现在我将我的行缓冲到Deque ,然后每 100 Deque整个Deque转储到文件中(数字是任意的)。 I currently using the Files.write() method.我目前使用Files.write()方法。

I would ideally like to open the file when the program begins and close the file when the program ends, but I don't know when the program is going to end, so I might fail to close the file.理想情况下,我希望在程序开始时打开文件并在程序结束时关闭文件,但我不知道程序何时结束,因此我可能无法关闭文件。 Is that even an issue?这甚至是一个问题吗? Should I do the buffering myself with the Deque , or should I use BufferedWriter or something else similar?我应该用Deque自己缓冲,还是应该使用BufferedWriter或其他类似的东西?

If you were content to risk losing the last 100 lines from a Deque when the application crashes, then you can also use a BufferedWriter and flush() it every 100 lines.如果您愿意冒险在应用程序崩溃时丢失Deque的最后 100 行,那么您还可以使用BufferedWriter并每 100 行flush()一次。

Flushing it has the effect of writing it to the operating system.刷新它具有将其写入操作系统的效果。

Technically, the operating system can also keep data in memory, but that also happens with a FileOutputStream without a buffer.从技术上讲,操作系统也可以将数据保存在内存中,但这也发生在没有缓冲区的FileOutputStream FileOutputStream.getFD().sync() forces the OS buffers to disk as well, if you are concerned about losing power or about an OS crash. FileOutputStream.getFD().sync()强制操作系统缓冲区到磁盘,如果您担心失去电源或操作系统崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM