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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:07:55+00:00 2026-06-15T19:07:55+00:00

I need some help please writing the output to a file and I can’t

  • 0

I need some help please writing the output to a file and I can’t get it to work. If I use the System.out.println it works. If I create the file stream and Buffered Writer in the actual method, it creates the file but doesn’t write anything to it. I’m assuming it’s because my method is recursive and creates a new file every time the method calls it self again. So I created another print method and used the string value key[i] as the string parameter and it does nothing.

Any help is appreciated, thank you.

 public void print(String s)throws IOException
{
    fstream = new FileWriter("out.txt", true);
    out = new BufferedWriter(fstream);

    try{
        out.write("From print: " + s + " ");
        out.close();
    }catch (Exception e){//Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
}

public void generate() throws IOException
{ 
    while (k<randomWordNum())
    {
        if (randomNum() <= sumOfFreq[0])
        {
            //System.out.println(getKey[0] + " "); 
            print(getKey[i]);
            i++;
            k++;
            generate();
        }
        if (randomNum() >= sumOfFreq[i] && randomNum() <= sumOfFreq[i+1])
        {
            //System.out.println("From generate: " + getKey[i+1] + " "); 
            print(getKey[i+1]);
            i++;
            k++;
            generate();
        }
        else
        {
            i++;
            generate();
        }
    }//while
}//generate
  • 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-15T19:07:57+00:00Added an answer on June 15, 2026 at 7:07 pm

    I think that constructor of FileWriter will overwrite the file. So you’ll need to use a code line like this:

    fstream = new FileWriter("out.txt", true); // true for appending
    

    Also, always close a file before it goes out of scope, otherwise it might never get flushed or closed if you are unlucky…

    And one more thing, assuming that is not some sort of debug/troubleshooting code, “never” catch Exception. If you do catch it, be sure to re-throw it asyou got it after logging or whatever you did with it. But, in general, always catch a more specific exception type.

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

Sidebar

Related Questions

i need some help writing a for-loop please but i can't get it. i
I need some help to get this right, please. I have created an iPad
I need some help with C++, please! I'm writing a command parser for a
Need some help, please. I have a line of horizontal thumbnails loaded as ONE
I need some help! Please. I am trying to develop a very very simple
I need some help with a LINQ query in VB.Net, please. I have this
I need some with help with PowerShell, please. It should be pretty easy: I
please i need you help with my script.. I'm trying to echo some content
I am writing a library where I need to use some constant integers. I
Can anyone please, point out some good tutorials, to develop a front end gui

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.