2013-06-27 48 views
1

全部。我尝试使用CsvParser解析CSV文件,但在第57行(〜6500)中读取后发生IOException:流关闭错误。有谁知道这可能是什么原因造成的?下面是一段代码和错误:CsvParser流关闭错误

#!/usr/bin/ groovy 

package csvTest 

@Grab ('com.xlson.groovycsv:groovycsv:1.0') 
import com.xlson.groovycsv.CsvParser 

def csvFile = new File("file.csv").withReader { 
    CsvParser.parseCsv(it) 
} 

csvFile.each { 
    println it 
} 

Caught: java.io.IOException: Stream closed 
java.io.IOException: Stream closed 
    at au.com.bytecode.opencsv.CSVReader.getNextLine(CSVReader.java:245) 
    at au.com.bytecode.opencsv.CSVReader.readNext(CSVReader.java:212) 
    at au.com.bytecode.opencsv.CSVReader$readNext.call(Unknown Source) 
    at com.xlson.groovycsv.CsvIterator.hasNext(CsvIterator.groovy:72) 
    at csvTest.CsvTest.run(CsvTest.groovy:12) 

回答

2

CsvParser很懒,所以读取请求时向它们行(而不是装载他们都到内存

withReader调用关闭Reader一旦闭合。完成

所以,当你尝试做csvFile.each,流被关闭

这应该工作:。

new File("file.csv").withReader { 
    def csvFile = CsvParser.parseCsv(it) 

    csvFile.each { 
     println it 
    } 
} 
+0

看起来像这样做的伎俩!谢谢! – Jess