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

  • SEARCH
  • Home
  • 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 9021847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:21:14+00:00 2026-06-16T05:21:14+00:00

I hope this is a straight forward design question. Context: I may be downloading

  • 0

I hope this is a straight forward design question.

Context:

I may be downloading one to many files over a socket connection. I am being passed byte[]s as they are read from the socket. I also know which file to write those bytes to. I am appending these bytes to the file with a FileOutputStream. I am also informed when all bytes for a file have been received.

Question:

Is it better to:

  1. keep a FileOutputStream open until all bytes have been received and written
  2. open a new FileOutputStream that appends the bytes to the proper file as they’re received and then closes each time.

2 feels safer to me because I will close the stream after each write in case something goes wrong (like I stop getting bytes for some reason) with any of the downloads. But it also seems not very efficient. I’m having trouble finding out how expensive opening and closing FileOutputStreams is. Are there any other side-effects to keeping the FileOutputStream open besides the extra care needed for knowing when to close it?

Thanks in advance.

  • 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-16T05:21:15+00:00Added an answer on June 16, 2026 at 5:21 am

    I agree with you that 2 is safer, but also much more expensive than 1.

    Whenever you create a FileOutputStream, it calls its native-implemented open() method. The implementation of this method will end up creating a file handle and issuing and open() system call to the operating system. This will involve at least synchronization (file creation at OS level is usually atomic) and probably some buffer allocation, thus it will be much more expensive than keeping the file handle open.

    On the other hand, if you use 1, you have the risk of mantaining too many open file handles and reaching the limit (which depends on the underlying operating system). And also of not closing the file if you don’t control all error paths.

    I would suggest evaluating how many open files you will have open in parallel, how difficult it would be to control all error paths to close the file, and the throughput you need for your server. Depending on the outcome of such analysis, I would go for 1 or 2.

    Additionally, if you use 1, you could implement some safe net, like a watchdog closing files which have not been written for some timeout.

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

Sidebar

Related Questions

Fairly straight-forward Ember question here, (I hope!). I have a simple Ember-data setup. One
I hope this question isn't too dumb. We have many websites internally that need
I hope this is a relatively straight forward thing to do, and that my
I will try to make this as straight forward as possible. This question does
Hope this question is not stupid since I am an amateur web designer. I
hope this will be a quick easy question, I've just tried my app on
I hope this is an easy one. Using MVC4 with Entity Framework 4.1. I'am
I hope this question isn't too general. Well, the situation is that I am
This should be relatively straight forward however I'm being swamped in SQL information whenever
I am sure this is a straight forward application, but not sure what the

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.