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

```