I'm using UNIVOCITY-PARSERS for converting csv file rows into java objects.
while processing the file, if it encounters any problem any of the column in row, then it parsing getting stopped in that row and throwing exception. But i need something which will continue till end of the file just by skipping the row which has error. But i didn't any utility classes in the api.
MY Bean class
public class ItemcodeBean {
@Trim
@NullString(nulls = { " ", "" })
@Parsed(field = "ItemCode")
private String itemCode;
@Trim
@NullString(nulls = { " ", "" })
@Parsed(field = "PartNumber")
private String partNumber;
@Trim
@NullString(nulls = { " ", "" })
@Parsed(field = "ModelNumber")
private String modelNumber;
}
My Main Class
public class TestClass {
private BeanListProcessor<ItemcodeBean>
rowProcessor = null;
private CsvParser parser = null;
public static void main(String[] args) {
TestClass testClass = new TestClass();
testClass.init();
try{
ItemcodeBean itemcodeBean;
while ((itemcodeBean = testClass.getRowData()) != null){
System.out.println(itemcodeBean.toString());
}
}catch (Throwable ex){
System.out.println(ex.getLocalizedMessage());
}
}
private BeanListProcessor<ItemcodeBean> init() {
// BeanListProcessor converts each parsed row to an instance of a given class, then stores each instance into a list.
this.rowProcessor =
new BeanListProcessor<ItemcodeBean>(ItemcodeBean.class);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
// skip leading whitespaces
parserSettings.setIgnoreLeadingWhitespaces(true);
//skip trailing whitespaces
parserSettings.setIgnoreTrailingWhitespaces(true);
//skip empty lines
parserSettings.setSkipEmptyLines(true);
File file = new File("C:\\Users\\abhishyam.c\\Downloads\\Itemcode_Template.csv");
this.parser = new CsvParser(parserSettings);
//parser.parse(file);
parser.beginParsing(file);
return rowProcessor;
}
private ItemcodeBean getRowData() throws Throwable {
String[] row;
try {
while ((row = parser.parseNext()) != null){
return rowProcessor.createBean(row, parser.getContext());
}
}catch (DataProcessingException e){
throw new DataProcessingException(e.getColumnName(),e);
}
// parser.stopParsing();
return null;
}
}