【发布时间】:2013-06-10 14:00:12
【问题描述】:
我正在使用 SuperCSV 将 CSV 记录解析为 Object。我的 CSV 文件最后有额外的列,我只想处理前 X 列。所以我为第一个 X 列定义了String[] 映射和相同大小的CellProcessor[]。但它似乎不起作用并抛出异常,即单元处理器的数量应与列数完全相同。
如果我遗漏了什么,谁能告诉我。我是否需要将映射数组定义为具有与五个完全相同的列,即使我不想要它们?
public CsvToBeanParser(Reader reader, Class<T> type, CsvPreference preference, CellProcessor[] cellProcessors, String[] mapping, boolean skipHeader)
throws IOException {
this.beanReader = new CsvBeanReader(reader, preference);
this.mapping = mapping;
if (skipHeader) {
beanReader.getHeader(true);
}
this.cellProcessors = cellProcessors;
this.type = type;
}
/**
* Parse and return record.
*
* @return
* @throws Exception
* if there is any parsing error
*/
public T getItem() throws Exception {
try {
return (T) beanReader.read(type, mapping, cellProcessors);
} catch (Exception e) {
LOG.error("Error parsing record", e);
throw e;
}
}
这是我的映射和单元处理器
String[] mapping = {"column1", "column2"};
CellProcessor[] cellProcessors = {null, null};
这适用于文件
column1, column2
1,2
但失败了(我想忽略 column3)
column1, column2, column3
1,2,3
【问题讨论】:
-
你能不能发一些代码给我们看看?
-
另外,您使用的是
AbstractCsvReader吗? -
我正在使用 CsvBeanReader