I am using SuperCSV to parse CSV records into Object. My CSV files have extra column in the end and I want to process only first X columns. So I define String[]
mapping for first X columns and CellProcessor[]
of same size. But it does not seem to work and throws exception that number of cell processors should be exactly same as number of columns.
Can somebody tell me if I am missing something. Do I need to define mapping array to have exact same columns as in five even if I don't want them?
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;
}
}
Here are my mapping and cell processors
String[] mapping = {"column1", "column2"};
CellProcessor[] cellProcessors = {null, null};
This works for file
column1, column2
1,2
but fails for (where I want to ignore column3 )
column1, column2, column3
1,2,3