CsvParserSettings s = new CsvParserSettings(); s.setHeaderExtractionEnabled(false); CsvRoutines r = new CsvRoutines(s); //set headers of input with 3 columns s.setHeaders("action" , "id", "name"); for(Bean b : r.iterate(Bean.class, new StringReader("I,123,Hello\nU,345,Hi"))){ System.out.println(b); } //set headers of input with 2 columns s.setHeaders("id", "name"); for(Bean b : r.iterate(Bean.class, new StringReader("123,Hello\n345,Hi"))){ System.out.println(b); }
public static void main(String[] args) throws IOException{ //creates a processor of java beans. BeanListProcessor<UserAttributeRecord> beanProcessor = new BeanListProcessor<UserAttributeRecord>(UserAttributeRecord.class); //then a settings object to configure the parser CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial //configures the parser to use the bean processor. settings.setRowProcessor(beanProcessor); //configures the input format. settings.setHeaders("userId", "attributeId", "rating", "timestamp", "email", "attributeDesc"); settings.getFormat().setLineSeparator("\n"); //creates a parser with your settings CsvParser parser = new CsvParser(settings); //parses everything. All rows are submitted to the row processor defined above parser.parseAll(new FileReader(new File("/path/to/file.csv"))); //here's your list of beans List<UserAttributeRecord> beans = beanProcessor.getBeans(); }