Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:38:22+00:00 2026-06-16T13:38:22+00:00

So I’m trying to write messages from users on a messaging network to a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:38:23+00:00Added an answer on June 16, 2026 at 1:38 pm

    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:

    • If you write something to the log, it will ‘really’ be written within 5 seconds.
    • If something else gets written before 5 seconds, it just gets added to the buffer (to be written when the time is up).

    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:

    • If you flush data to disk every time anything gets written, but load increases and there are, say, 10,000 events per second, then you’ll be wasting I/O time with 10,000 disk writes per second.
    • If you leave it to Java/the OS to decide when to flush data, but load is very low (e.g. in middle of the night), the log could even be hours out of date. (If there is one event, not big enough to fill the buffer, then nothing for hours.)

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.