So, I am trying to write messages from users on a messaging network to a file. I am trying to create this program with good java practices and a suitable file I / O technique.
Currently, my program recognizes that someone has sent a message, receives the message, and immediately writes it to a file. Create a file object, create a recording object, add a message, and then close the file. This seems like good practice if not many messages arrive in the message, but if there is a fast conversation flow, it seems slow and requires a lot of unnecessary actions, because the file will be opened again immediately.
Then I thought that if I just left the file open and just wrote messages when they arrived at the file, then closed it periodically. Is this a good practice? Keeping a file open for a long time? For example, an hour or after a certain amount of data has been recorded?
Now I think that I should accept messages, store them in the cache (for example, a string array), and then save the array of strings to a file when the "cache" is full. Is this the best practice?
I have two questions:
1) Is it good to leave the file open for a long period of time (from several minutes to several hours) if you are not using the file?
2) What is the good practice for the "cache" that I'm talking about? Is a string array good? Is there anything better I should use? How are you going to store this information?
source share