Question

I have Groovy code like this...

class GuiWindow extends SwingBuilder {

....
FileWriter writer

    GuiWindow(def controller)
{
    ....
    writer = new FileWriter("DATA${new Date().time.toString()}.txt")
    writer.write("Count, Rate\n")

This code successfully creates the file but seemingly refuses to write anything into it - not even the header line of text in the immediate next statement.

I have used very similar code before without any problem so am at a bit of a loss as to know what is wrong.

I don't close() the writer - it is meant to be open to record data which comes in slowly - so the one thing I can think of is that the writing is batched and only written when a buffer overflows - but can someone tell me what is wrong with my code or why it doesn't behave the way I expect it to?

Was it helpful?

Solution

Just like tim_yates mentiones above, flush solved the problem in my case.

FileWriter writer = new FileWriter("DATA${new Date().time.toString()}.txt")
writer.write("Count, Rate\n")
writer.flush()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top