uniVocity-parsers不要求你在你的類提供的字段名,但它使用的註解,如果你需要確定一個不同的名字,甚至數據操作進行。它也比你嘗試過其他庫的方式更快:
class TestBean {
@NullString(nulls = { "?", "-" }) // if the value parsed in the quantity column is "?" or "-", it will be replaced by null.
@Parsed(defaultNullRead = "0") // if a value resolves to null, it will be converted to the String "0".
private Integer quantity; // The attribute name will be matched against the column header in the file automatically.
@Trim
@LowerCase
@Parsed
private String comments;
...
}
爲了解析:
BeanListProcessor<TestBean> rowProcessor = new BeanListProcessor<TestBean>(TestBean.class);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(parserSettings);
//And parse!
//this submits all rows parsed from the input to the BeanListProcessor
parser.parse(new FileReader(new File("/examples/bean_test.csv")));
List<TestBean> beans = rowProcessor.getBeans();
披露:我是這個庫的作者。它是開放源代碼和免費的(Apache V2.0許可證)。