Choose between OpenCSV and Apache Commons CSV

OpenCSV vs Apache Commons CSV for Java: how they differ on bean mapping, CSV dialects, TSV, escaping, dependencies and licensing, and which one fits your project.

The short answer

Choose OpenCSV if you want CSV rows mapped directly to and from Java objects with annotations, plus built-in validation. Choose Apache Commons CSV if you want a lean record-based parser and printer with named CSV dialects such as Excel, RFC 4180, tab-delimited and database export formats, and you are comfortable writing your own mapping code. Both are free, open-source Java libraries released under the Apache License 2.0, so price is not a deciding factor.

Both libraries convert between delimited text and structured data. They change structure, such as rows, fields, quoting and delimiters. They do not change how characters look. Whether the output works depends on dialect settings, character encoding and the program that opens the file.

How the two libraries differ

Criterion | OpenCSV | Apache Commons CSV
Main abstraction | Arrays of strings per line, or annotated Java beans | CSVRecord objects, accessed by index or header name
Object mapping | Built in (annotations such as CsvBindByName and CsvBindByPosition; CsvToBean and StatefulBeanToCsv) | Not included; you map records to objects yourself
Dialect configuration | Separator, quote and escape characters set through builders; separate RFC 4180 parser available | Predefined CSVFormat constants (for example DEFAULT, EXCEL, RFC4180, TDF, MYSQL) that you can customize
TSV support | Set the separator to a tab character | Use the TDF format
Dependencies | Pulls in several other Apache Commons libraries | Small footprint; check the dependency tree for your version
License | Apache License 2.0 | Apache License 2.0

No benchmarks, version numbers or release dates were supplied for this comparison. Before committing, check each project's current release, changelog and open issues. Do not rely on older performance claims.

Which option fits your requirements

  • Use OpenCSV when your code mainly reads files into domain objects or writes objects out, and you want annotations to replace hand-written mapping.
  • Use OpenCSV when you need field-level conversion or validation tied to bean properties, and you accept extra transitive dependencies.
  • Use Apache Commons CSV when you handle many CSV dialects, or files from spreadsheets and database exports, and want each dialect expressed as one named format.
  • Use Apache Commons CSV when dependency size matters, for example in shared libraries or constrained deployments, or when you prefer explicit, readable mapping code.
  • Use either one, or neither, for very simple, controlled data. If fields never contain delimiters, quotes or line breaks, a basic split may work. Once the input comes from users or other systems, a real parser is the safer choice, and both libraries cost nothing to adopt.

Switching libraries later mostly means rewriting mapping code. If you already use OpenCSV annotations throughout a codebase, moving to Commons CSV means writing that mapping by hand. Moving the other way mainly means adding annotations and accepting more dependencies.

Typical usage

OpenCSV bean mapping: annotate fields with header names, then parse a reader into a list of objects.

public class Person {
    @CsvBindByName(column = "name")
    private String name;

    @CsvBindByName(column = "age")
    private int age;
}

List<Person> people = new CsvToBeanBuilder<Person>(reader)
        .withType(Person.class)
        .build()
        .parse();

Apache Commons CSV record access: choose a format, treat the first line as a header, and read fields by name. The builder method names have changed across releases, so check the Javadoc for your version.

CSVFormat format = CSVFormat.DEFAULT.builder()
        .setHeader()
        .setSkipHeaderRecord(true)
        .build();

try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8);
     CSVParser parser = format.parse(in)) {
    for (CSVRecord record : parser) {
        String name = record.get("name");
    }
}

Limits that affect readability and reuse

Escaping is the most common surprise with OpenCSV. Its default parser treats the backslash as an escape character, which can corrupt values that contain backslashes, such as Windows file paths. If your data follows RFC 4180, where quotes are escaped by doubling them, configure the RFC 4180 parser instead:

CSVReader csvReader = new CSVReaderBuilder(reader)
        .withCSVParser(new RFC4180ParserBuilder().build())
        .build();
  • Encoding: both libraries read from Java Readers, so your code chooses the character set. Pick it explicitly, usually UTF-8, rather than relying on the platform default.
  • Byte order marks: files saved by some spreadsheet tools start with a BOM, which can end up attached to the first header name and break lookups by name. Wrapping the input in a BOM-aware stream is a common fix. Confirm how your library version behaves with a small test file.
  • Dialect mismatch: a file written with one delimiter, quote or line-ending convention may parse wrongly with another. Agree on the dialect with whoever produces or consumes the file.
  • Spreadsheet destinations: how a given spreadsheet application handles encoding, delimiters and leading zeros on import was not documented here. Test with the exact application and version your readers use.

Both libraries produce ordinary plain text. That makes the output easy to copy, diff and process with other tools. Screen-reader behavior depends on the application that displays the data, not on the library, and no assistive-technology testing was supplied, so verify it in your target environment if it matters.

Handling sensitive data

Both libraries run inside your own application. Neither sends data to an outside service by itself, so sensitive records stay wherever your code keeps them. Watch for one risk: if exported files will open in a spreadsheet, values starting with characters such as =, +, - or @ may be interpreted as formulas. Neither library should be assumed to neutralize these values automatically. Sanitize or prefix them in your own code when the file could carry untrusted input.

What to check before choosing

  • Whether you need annotation-based object mapping or prefer explicit record handling.
  • Which CSV or TSV dialects your inputs and outputs actually use, including escape and quote rules.
  • The current release, maintenance activity and changelog of each project.
  • The transitive dependencies each library adds, and any conflicts with versions already in your build.
  • A round-trip test using real sample files: embedded commas, quotes, line breaks, backslashes, non-ASCII text and a BOM.