我正在尝试阅读大CSV
和TSV
(Tab sepperated)具有约1000000
行或更多行的文件。现在我试着读TSV
含~2500000
行与opencsv
,但它会抛出我。它适用于较小的TSV
含~250000
行的文件。所以我想知道是否有任何其他Libraries
,支持巨大的CSV
和TSV
文件的阅读。你有什么想法?适用于Java的良好和有效的CSV/TSV阅读器
大家谁是有兴趣在我的代码(我缩短了,所以Try-Catch
显然是无效的):
InputStreamReader in = null;
CSVReader reader = null;
try {
in = this.replaceBackSlashes();
reader = new CSVReader(in, this.seperator, '\"', this.offset);
ret = reader.readAll();
} finally {
try {
reader.close();
}
}
编辑:这是我构建InputStreamReader
方法:
private InputStreamReader replaceBackSlashes() throws Exception {
FileInputStream fis = null;
Scanner in = null;
try {
fis = new FileInputStream(this.csvFile);
in = new Scanner(fis, this.encoding);
ByteArrayOutputStream out = new ByteArrayOutputStream();
while (in.hasNext()) {
String nextLine = in.nextLine().replace("\\", "/");
// nextLine = nextLine.replaceAll(" ", "");
nextLine = nextLine.replaceAll("'", "");
out.write(nextLine.getBytes());
out.write("\n".getBytes());
}
return new InputStreamReader(new ByteArrayInputStream(out.toByteArray()));
} catch (Exception e) {
in.close();
fis.close();
this.logger.error("Problem at replaceBackSlashes", e);
}
throw new Exception();
}
为什么不用BufferedReader自己读取? –
其实我想要的是精心制作的,常用的Code,我不想重新发明轮子,实际上这就是每个人都在使用libs的原因。但如果没有任何工作,我会这样做。 – Robin
有很多行,我会分批处理文件:从文件中读取n行,使用csv处理,读取下一批等。 – opi