Jess
Jess

Reputation: 227

Writing to file stops suddenly in Groovy

I am using newWriter() to write to a file from a Groovy script. Everything seems to be going fine, but then the script suddenly stops writing to the file even though print statements show that the script does get through the complete list of objects I'm trying to write out. The file gets to line 461 and then just stops writing mid-sentence. The file size is 8.2 kB. I write to the file in the following way:

def file = new File('fileName.json')
def f = file.newWriter()
f << "..."

To create the content of the file, I have to loop through a list of objects. So, I first write some header information using "f <<" and then I continue to write more information from within a loop in the same way. The file stops getting written to from within this loop even though the print statement I have in the loop prints for all objects in the list.

Upvotes: 2

Views: 170

Answers (1)

ataylor
ataylor

Reputation: 66069

File.newWriter returns a java.io.BufferedWriter. It's possible the remaining information is sitting in the write buffer. Try calling flush or close on the writer to force it to be written.

Upvotes: 2

Related Questions