Apache Commons CSV vs OpenCSV: Which Java CSV Library Fits Your Job
Compare Apache Commons CSV and OpenCSV by parsing model, quoting and escaping defaults, TSV support, bean mapping and output compatibility, then choose for your situation.
Apache Commons CSV and OpenCSV are both open-source Java libraries for reading and writing delimited text. They overlap heavily, so the useful question is which one's defaults and abstractions match your data and your destination. This guide compares them on what they change in your text, how faithfully their output survives the trip to spreadsheets, databases or other programs, and which situations favor each.
Evidence note: no benchmarks, version-specific test results or release dates were supplied for this comparison. The descriptions below reflect each project's widely documented design. Behavior can change between releases, so confirm defaults against the documentation for the exact version you plan to use.
What a CSV library actually changes in your text
Neither library reorders, restyles or reinterprets your characters. Their job is narrower: splitting a stream of text into records and fields when reading, and joining fields back into delimited text when writing. Changes happen only at the structural layer, but they still matter:
- Quoting: a field that contains the delimiter, a quote character or a line break must be wrapped in quotes, and the library decides when to add or remove them.
- Escaping: embedded quotes are either doubled (the RFC 4180 convention) or prefixed with an escape character such as a backslash. A mismatch here silently corrupts values.
- Record separators: the library chooses whether rows end with CRLF or LF when writing, and which line endings it accepts when reading.
- Whitespace and empty values: options may trim spaces, treat empty strings as null, or skip blank lines.
- Headers: the first row may be treated as data or as column names used for lookup.
Because these rules differ between libraries and between configurations of the same library, the same input file can produce different field values. That is the core of the comparison.
Apache Commons CSV at a glance
Commons CSV is part of the Apache Commons project. Its design centers on a format object, CSVFormat, which bundles the delimiter, quote character, escape character, record separator, header handling and related options. Predefined formats cover common dialects, including RFC 4180, a default variant, Excel-style CSV, tab-delimited text (TDF) and several database export formats. You parse with CSVParser, iterate over CSVRecord objects, and write with CSVPrinter.
The strength of this model is explicitness: the dialect is a named, configurable object rather than a set of scattered defaults, which makes it easier to state exactly which format a pipeline expects. It works with rows as records of strings; it does not map rows to Java objects, so any conversion to typed fields is your code's responsibility.
// Commons CSV: read an RFC 4180 file (illustrative)
try (Reader in = Files.newBufferedReader(path, StandardCharsets.UTF_8);
CSVParser parser = CSVParser.parse(in, CSVFormat.RFC4180)) {
for (CSVRecord record : parser) {
String first = record.get(0);
}
}
OpenCSV at a glance
OpenCSV offers two layers. The lower layer, CSVReader and CSVWriter, works with rows as string arrays. The higher layer binds rows to Java beans, using annotations or mapping strategies by column name or position, and can convert field values to typed properties. For applications that read CSV directly into domain objects, this saves a meaningful amount of hand-written mapping code.
One documented detail deserves attention: OpenCSV ships more than one parser. Its default CSVParser treats the backslash as an escape character, while its RFC4180Parser follows the doubled-quote convention without backslash escaping. If your files come from tools that write Windows paths or other text containing backslashes, the default parser can alter those values. Choose the parser deliberately rather than relying on the default.
// OpenCSV: read rows as string arrays (illustrative)
try (CSVReader reader = new CSVReaderBuilder(
Files.newBufferedReader(path, StandardCharsets.UTF_8))
.withCSVParser(new RFC4180ParserBuilder().build())
.build()) {
String[] row;
while ((row = reader.readNext()) != null) {
String first = row[0];
}
}
Exact method signatures and checked exceptions have shifted across OpenCSV releases, so treat both snippets as sketches to adapt rather than drop-in code.
Side-by-side comparison
| Aspect | Apache Commons CSV | OpenCSV |
| Core model | Named, configurable CSVFormat plus parser and printer | Reader and writer over string arrays, plus a bean-binding layer |
| Row representation | CSVRecord, accessed by index or header name | String array, or a mapped Java bean |
| Object mapping | Not included; write your own | Built in, via annotations or mapping strategies |
| Dialect presets | Several predefined formats, including RFC 4180, Excel and tab-delimited | Configured through parser and writer builders |
| Escaping default to check | Depends on the chosen CSVFormat | Default parser uses backslash escaping; RFC4180Parser does not |
| TSV support | Predefined tab-delimited format | Set the separator to a tab character |
| License | Apache License 2.0 | Apache License 2.0 |
| Performance | No benchmark evidence supplied | No benchmark evidence supplied |
Will the output stay readable and usable where you need it?
CSV and TSV are plain text, so they copy, paste and transfer without losing styling, because there is none to lose. The risks are interpretive: the receiving program may read the structure, encoding or individual values differently than you intended. Check these points against your actual destination.
Character encoding and byte order marks
Both libraries operate on Java Readers and Writers, so the character encoding is set by how you open the stream, not by the library. Classes such as FileReader historically used the platform default encoding, which varies between machines; opening streams with an explicit charset such as UTF-8 avoids that. Some spreadsheet programs expect a byte order mark to detect UTF-8, while many parsers treat a leading BOM as part of the first header name. Commons CSV's documentation recommends stripping a BOM upstream, for example with a BOM-aware input stream. If you write files for a spreadsheet, test whether your target needs a BOM rather than assuming.
Line endings
RFC 4180 specifies CRLF line endings, and Commons CSV's RFC 4180 and default formats write CRLF. OpenCSV's CSVWriter has historically defaulted to LF. Most modern tools accept either, but strict consumers and byte-for-byte comparisons in tests do not. Set the record separator explicitly when the destination has a requirement.
Quoting compatibility between producer and consumer
The most common source of corrupted fields is a writer and reader that disagree on escaping. A file written with backslash escaping and read by an RFC 4180 parser, or the reverse, will mangle values containing quotes or backslashes. Whichever library you choose, configure both ends of the pipeline to use the same dialect, and include test rows with embedded delimiters, quotes, backslashes and line breaks.
TSV specifics
Tab-separated files are often produced without any quoting, on the assumption that fields never contain tabs or newlines. If your data can contain those characters, decide whether to quote them, escape them or reject them, and confirm the consuming tool handles that choice. Converting between CSV and TSV with either library is a matter of reading with one dialect and writing with the other, which is safe only when the quoting rules on both sides are set explicitly.
Spreadsheet formula injection
When a CSV file is opened in a spreadsheet, a cell beginning with characters such as an equals sign, plus, minus or at sign may be evaluated as a formula. This is a property of the spreadsheet, not the CSV format, and neither library should be assumed to neutralize it by default. If you export user-supplied data that people will open in a spreadsheet, sanitize or prefix risky values in your own code and verify the result in the target application.
Sensitive data
Both are libraries that run inside your own application, so data is not sent anywhere unless your code sends it. That differs from pasting files into online CSV converters, which transmit content to a third party. For confidential data, processing locally with either library avoids that exposure.
Which one to choose
- Choose Apache Commons CSV when you want the file dialect defined explicitly in one place, you work with rows as records, you need ready-made presets for formats such as Excel, RFC 4180 or tab-delimited, or your project already relies on Apache Commons components.
- Choose OpenCSV when you want rows mapped directly to Java objects with typed fields, and you prefer annotations over hand-written conversion code.
- Either works for straightforward reading and writing of well-formed files. In that case, pick the one already present in your dependency tree to avoid adding another library.
- If throughput matters, benchmark both against your own files and hardware; no comparative performance data was supplied for this guide.
Checks before you commit
- Read the documentation for the exact version you will deploy and confirm the default delimiter, quote, escape character and record separator.
- Inspect the dependency tree each library pulls into your build, since transitive dependencies vary by release.
- Run a round-trip test: write sample data, read it back, and compare field by field, including empty values, embedded quotes, backslashes, delimiters, line breaks and non-ASCII characters.
- Open generated files in the actual destination, such as a specific spreadsheet program or database import tool, rather than assuming compatibility.
Gaps in this comparison
This guide does not include measured parsing speed, memory use, release history or maintenance activity for either project, because no such evidence was supplied. Treat any claim elsewhere that one library is universally faster or more compatible as something to verify with your own data.