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 8488291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:29:27+00:00 2026-06-10T21:29:27+00:00

Here is my snippet. File file = new File(Thread.currentThread().getName()); for(each element of the list){

  • 0

Here is my snippet.

File file = new File(Thread.currentThread().getName());

for(each element of the list){
createStringWriterAndPopulateDataToBeWritten
FileOutputStream fo = new FileOutputStream(file);
OutputStreamWriter out = new OutputStreamWriter(fo,"UTF-8");
out.write(sw.toString());
out.close();
}

Now say i have a newFixedThreadPoolof size S; i pass this thread pool a list of work to be done. Now everytime a thread is called, it creates a file with name as the name of thread and then writes to it, but in the loop it just overwrites the file.

How can make it work to append and not overwrite, even if the Thread is called again, which can very much happen in this case since my list of Work to the thread pool is pretty long.

Note:Each element of the list of jobs passed to the ThreadPool is also a list and that is why there a for loop in the snippet.

Thanks.

  • 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-10T21:29:29+00:00Added an answer on June 10, 2026 at 9:29 pm

    but in the loop it just overwrites the file.

    Well yes – you’re creating the FileOutputStream in the loop. If you create the FileOutputStream (and the OutputStreamWriter) before the loop, then just do the writing in the loop, it will create a single file for the whole operation.

    Note that the close call should be in a finally block so you close the writer even if the code throws an exception.

    If you also need to append to the file if this code is called again, just change this:

    FileOutputStream fo = new FileOutputStream(file);
    

    to

    FileOutputStream fo = new FileOutputStream(file, true);
    

    The second argument, as per the documentation, states whether to append (true) or overwrite (false).

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

Sidebar

Related Questions

I'm reading a csv file using c# here is a little code snippet. using
Here is the code snippet. read = new FileReader(trainfiles/+filenames[i]); br = new BufferedReader(read); while((lines
Here is a snippet of the XML file I am trying to read. <?xml
Here is a code snippet where I am opening a file for writing with
Here's a snippet of code I saw on the web and I'm wondering if
Here's a snippet from my application: class PortolioItem(models.Model): ... user = models.ForeignKey(User) contract =
Here's a snippet of a script of mine attempting to put extra Unicode awesomeness
Here is a snippet of the official Apple Documentation of AudioBufferList (Core Audio Data
Here is a snippet of my code $fp = fsockopen($s['url'], 80, $errno, $errstr, 5);
Here is a snippet from a Sinatra app where users will be submitting urls.

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.