Attempt to invoke method: write() on undefined variable or class name

Hi,

I'm having some problems with outputing data from my scraping session. I am scraping a particular page for a list of sub-pages, then scraping the sub-pages. From the sub-pages I am extracting single elements, such as the date and time, as well as variable number of elements such as the names of commenters.

I would like to have the data from each sub-page printed to a unique file, but I am having problems with the opening or printing to the file.

What I have done thus far (I think) is to open a file before the sub-page is scraped, run the scraper, then close the file:

OpenFile Script, before file is scraped

out = new FileWriter("/Users/me/Desktop/outData/" +session.getVariable("Post_Number") +".txt");

test_out Script, run for each date Extraction

out.write("test\n");

CloseFile Script, run after the file is scraped
out.close();

The file is successfully created as expected. The error I get is

Processing script: "test_out"
An error occurred while processing the script: test_out
The error message was: class bsh.EvalError (line 1): out .write ( "test\n" ) -- Attempt to invoke method: write() on undefined variable or class name: out

Of course, the test_out script will be replaced with a good output script for each of the extractors once I get this working. I have confirmed through the logs that the data is extracted correctly, so there is no problem there that I can see.

I assume that the "out" output stream is only locally defined, so isn't in scope when running the extractors, although I could certainly be wrong. How do I correct this problem?

Thanks so much for any help!

//----------
Edit:

It looks like WriteValueToFile() is similar to what I need. However, multiple calls of WriteValueToFile() overwrites the file, rather than appending. Is there a similar method that allows appending to a file?

In our sample scripts, there

In our sample scripts, there is a write to CSV you could look at. You would just want to define the out variable each time the script is run.