How to read CSV File using Super Csv with CsvBeanReader

You may also like...

  • Jerry Bakster

    Thanks for the article, and I’m sharing one more open-source library uniVocity-parsers (http://www.univocity.com/pages/parsers-tutorial) for parsing CSV data. In my project, I found it powerful and flexiable, especially when parsing big CSV data (such as 1GB+).

    The library provides simplified API, and full capabilities in parsing CSV file, such as quote espacing, headers extraction, column selection, mapping to Java Beans and so on.

    Here is a code snippt for using this library:


    public static void main(String[] args) throws FileNotFoundException {
    /**
    * —————————————
    * Read CSV rows into 2-dimensional array
    * —————————————
    */
    // 1st, config the CSV reader, such as line separator, column separator and so on
    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setLineSeparator(“\n”);

    // 2nd, creates a CSV parser with the configs
    CsvParser parser = new CsvParser(settings);

    // 3rd, parses all rows from the CSV file into a 2-dimensional array
    List resolvedData = parser.parseAll(new FileReader(“/examples/example.csv”));

    /**
    * ———————————————
    * Read CSV rows into list of beans you defined
    * ———————————————
    */
    // 1st, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor rowProcessor = new BeanListProcessor(ColumnBean.class);
    settings.setRowProcessor(rowProcessor);
    settings.setHeaderExtractionEnabled(true);

    // 2nd, parse all rows from the CSF file into the list of beans you defined
    parser.parse(new FileReader(“/examples/example.csv”));
    List resolvedBeans = rowProcessor.getBeans();
    }

    Also find more details at official Github repository.