Below you can find how to parse .csv file with univocity library.
public class ExampleCsvToPojoParser {
private final Logger logger = LoggerFactory.getLogger(CsvToPojoParser.class);
public <T> List<T> parseFileToPojo(Class<T> clazz, String fileName) {
BeanListProcessor<T> rowProcessor = new BeanListProcessor<T>(clazz);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.setLineSeparatorDetectionEnabled(true);
parserSettings.setSkipEmptyLines(true);
CsvParser parser = new CsvParser(parserSettings);
try (InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(fileName)) {
parser.parse(resourceAsStream);
} catch (IOException e) {
logger.error("Cannot read file " + fileName, e );
}
return rowProcessor.getBeans();
}
}
Then you can put there class like this (which will match the file):
public class Users {
@Trim
@NullString(nulls = { " ", "" })
@Parsed(field = "USER_ID")
private Long userId;
@Trim
@NullString(nulls = { " ", "" })
@Parsed(field = "USER_NAME")
private String userName;
//getters & setters
}