- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试读取包含大约 1000000
行或更多行的大型 CSV
和 TSV
(制表符分隔)文件。现在我尝试读取包含 ~2500000
行的 TSV
和 opencsv
,但它抛出一个 java.lang.NullPointerException
。它适用于具有 ~250000
行的较小 TSV
文件。所以我想知道是否还有其他 Libraries
支持读取巨大的 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();
}
最佳答案
不要使用 CSV 解析器来解析 TSV 输入。例如,如果 TSV 包含带引号字符的字段,它将中断。
uniVocity-parsers带有 TSV 解析器。您可以毫无问题地解析十亿行。
解析 TSV 输入的示例:
TsvParserSettings settings = new TsvParserSettings();
TsvParser parser = new TsvParser(settings);
// parses all rows in one go.
List<String[]> allRows = parser.parseAll(new FileReader(yourFile));
如果您的输入太大以至于无法保存在内存中,请执行以下操作:
TsvParserSettings settings = new TsvParserSettings();
// all rows parsed from your input will be sent to this processor
ObjectRowProcessor rowProcessor = new ObjectRowProcessor() {
@Override
public void rowProcessed(Object[] row, ParsingContext context) {
//here is the row. Let's just print it.
System.out.println(Arrays.toString(row));
}
};
// the ObjectRowProcessor supports conversions from String to whatever you need:
// converts values in columns 2 and 5 to BigDecimal
rowProcessor.convertIndexes(Conversions.toBigDecimal()).set(2, 5);
// converts the values in columns "Description" and "Model". Applies trim and to lowercase to the values in these columns.
rowProcessor.convertFields(Conversions.trim(), Conversions.toLowerCase()).set("Description", "Model");
//configures to use the RowProcessor
settings.setRowProcessor(rowProcessor);
TsvParser parser = new TsvParser(settings);
//parses everything. All rows will be pumped into your RowProcessor.
parser.parse(new FileReader(yourFile));
披露:我是这个图书馆的作者。它是开源且免费的(Apache V2.0 许可)。
关于java - 适用于 Java 的优秀且有效的 CSV/TSV 阅读器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13879967/
在单元格中有这样的文字:Sum(d5:d10),请注意没有“=”。我想在另一个带有“=”的单元格中使用此文本,并计算范围 d5:d10 之和的结果,我想要的返回不是文本。 我的目的不是计算总和,但我想
我在 java 中创建了这个方法,用于指示整数数组是否已排序。它的复杂性是什么?我想如果好的是最坏情况下的 O(1) 是平均情况下的 O(n)? static boolean order(int[]
1.什么样的人,才能称得上“优秀”? 其实就看这三点: (1)普通的人改变结果; (2)优秀的人改变原因; (3)顶级优秀的人改变模型。 2.举个例子: 有一家公司为员工提供午餐和晚餐。 吃着吃着,大
我在表中有一个具有以下公式的列 =IF([@[JoBM]]>0; IF([@[JoBF]]>0;[@[Median1]]/[@[Median2]];"-");"-") 该列看起来像这样:0.9、0.8
根据他们的工作,您如何区分出色的 SQL 开发人员? 示例可能包括: 很少使用 CURSOR,并试图将它们重构掉。 很少使用临时表,并试图将它们重构掉。 自信地处理 OUTER JOIN 中的 NUL
我是一名优秀的程序员,十分优秀!