我试图解析一个CSV文件列表,我通过在目录上递归获得。下面打印的代码只有一条线,然后存在与流闭合差:CsvParser流解析多个文件时出现关闭错误
def stageDir = new File(STAGE_DIR), listOfFiles = [], filesData = []
// Get the list of files
stageDir.eachFileRecurse(FileType.FILES) { file ->
listOfFiles << file
}
// Start parsing each CSV file in the list
listOfFiles.each { file ->
def csvData = file.withReader {
new CsvParser().parse(it , separator: CSV_SEPARATOR)
}
// Here i put the returned csvData (of type CsvIterator) in the filesData
filesData.add(csvData)
}
// I checked filesData and it's not empty, so i iterate over it.
// HERE I GET : Stream Closed error
// I'm just trying to test by printing the first line of myIt
// (but is there a more efficient way to iterate over CsvIterator ?)
for (myIt in filesData) {
println myIt[0].ID + " " + myIt[0].NAME
}
是否有使用CsvParser多个文件,避免流关闭错误一个更好的办法吗?
仍然收到java.io.IOException:Stream在同一行上关闭:println myIt [0] .ID +“”+ myIt [0] .NAME! – amrfaissal
好的。网络上的任何示例项目?准备尝试? – Opal
我看过的示例处理一个文件和一个返回的CsvIterator不是多个文件:( – amrfaissal