Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting CSV file: IOException: Stream closed
    primarykey
    data
    text
    <p>My Grails app reads data from a database and write the data to a CSV file. Recently, it has been failing with the following error:</p> <pre><code>java.io.IOException: Stream closed at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:98) at java.io.BufferedWriter.write(BufferedWriter.java:203) at java.io.Writer.write(Writer.java:140) at sun.reflect.GeneratedMethodAccessor1116.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) </code></pre> <p>The error happens when I do the following (writer.write(builder.toString()):</p> <pre><code>Writer writer String delimiter =',' Pattern p public CsvGenerator() { p = Pattern.compile(delimiter) } public CsvGenerator(String fullPath) { writer = openWriter(fullPath) p = Pattern.compile(delimiter) } public CsvGenerator(String fullPath, String delimiter) { writer = openWriter(fullPath) this.delimiter = delimiter p = Pattern.compile(this.delimiter) } protected Writer openWriter(String fullPath) { Writer writer = new BufferedWriter(new FileWriter(fullPath)) return writer } public void closeWriter() { if (writer !=null) { writer.close() } } public void writeLine(List&lt;Object&gt; entries) { log.info "${entries}" StringBuilder builder = new StringBuilder() entries.eachWithIndex { Object entry, int index -&gt; builder.append(entry ==null? '': stripChars(entry.toString())) //writer.write(entry ==null? '': stripChars(entry.toString())) if (index &lt; entries.size()-1) { builder.append(delimiter) //writer.write(delimiter) } } builder.append('\r\n') writer.write(builder.toString()) } private String stripChars(String s) { String result =s.trim() return p.matcher(s).replaceAll('') //result.replace(delimiter,'') } </code></pre> <p>I've read that the cause is the write has been closed between writes. Is that correct?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload