So I’m trying to write messages from users on a messaging network to a file. I’m trying to build this program with good java practices and appropriate file IO technique.
Currently my program recognizes someone has posted a message, takes the message and immediately writes it to a file. Creating the file object, creating the writer object, appends the message, then closes the file.This seems like good practice if there aren’t many messages coming in, but if there is a rapid stream of conversation this seems slow and requires a lot of unnecessary actions because the file is going to opened again immediately.
Then I thought what if I just left the file open and just wrote the messages as they came to the file, then closed it periodically. Is that good practice? Leaving a file open for extended periods of time? For instance after an hour or after some amount of data has been written?
Now, I’m thinking I should take the messages, store them in a “cache”(like a string array), then save the string array to a file when the “cache” is full. Is this better practice?
So I have two questions:
1) Is it good practice to leave a file open for an extended period of time( a few minutes to a few hours) if you aren’t using the file?
2) What is good practice for a “Cache” like I’m talking about? Is a string array good? Is there something better I should use? How would you go about storing this information?
In my opinion, best practice for logs (and similar) in server applications is to decide an acceptable time delay and stick to it. For example, if you set a 5 second delay, write code so that:
That way, you only do at maximum one disk write per 5 seconds, but it is definitely written. This compares well to the other approaches:
I haven’t looked at the APIs recently to see if there is a built-in way to do this strategy but it is easy to code. By the way, there is no need to manually cache output; you can just use a BufferedOutputStream, and call the flush() object whenever you want to write it to disk. (That way it’ll also write automatically when it hits the buffer limit, but that’s probably OK if you pick the limit sensibly.)
Regarding leaving a file open, you can leave files open as long as you like (just close it when you are not going to write to it any more). Assuming you don’t have thousands of files open, and you don’t need to have multiple applications writing to the same file, this doesn’t cause any problems.