Read CSV file in java

A comma-separated values (CSV) (also sometimes called character-separated values, because the separator character does not have to be a comma) file stores tabular data (numbers and text) in plain-text form.

In this example, we will try to read string that is compose by 2 rows of record.

Example Output:

2 thoughts on “Read CSV file in java

  1. Try not to re-invent wheel when you need to fix common problems.

    As per the CSV file parsing task, you could use one of the best library uniVocity-parsers( with simplified API and excellent performance.
    In my project, it is used to parse CSV data more than 1GB+.

    Here are the code snippts for reading csv data into list of arrays:

    // 1st, config the CSV reader with line separator
    CsvParserSettings settings = new CsvParserSettings();

    // 2nd, config the CSV reader with row processor attaching the bean definition
    BeanListProcessor rowProcessor = new BeanListProcessor(Employee.class);

    // 3rd, parse all rows from the CSF file into the list of beans you defined
    CsvParser parser = new CsvParser(settings);
    parser.parse(new FileReader(“/examples/employees.csv”));
    List resolvedBeans = rowProcessor.getBeans();

    • This is just-an-example. The reason I process line by line is because I have very limited RAM on the machine. Of cause, reading a 1GB+ file into a List(memory) is one way of doing it. Thanks for stopping by and thanks for the great comment. Will consider using your uniVocity parser in the future.

Leave a Reply to justexample Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">