Reputation: 571
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). I currently using the Files.write()
method.
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?
Upvotes: 0
Views: 684
Reputation: 31269
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.
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.getFD().sync()
forces the OS buffers to disk as well, if you are concerned about losing power or about an OS crash.
Upvotes: 1