CSV4J is a simple CSV reader and writer for Java 8 or later.
- Provide CSV reading and writing functionality compatible with both de-facto and official standards
- Expose low-level encoding data
- Simple, compact API
- Object mapping
- Header handling
There are already many good libraries with CSV support available: Apache Commons CSV, Super CSV, Opencsv, Jackson, etc. So why publish another?
In my experience, most of these libraries either:
- Have long-standing bugs
- Are complex to use
- Do too much
- Do not give visibility into low-level encoding data, particularly whether individual fields are quoted
- Do not handle byte order marks (BOMs)
In response, this library is designed to be:
- Correct
- Simple
- Focused
- Transparent
- BOM-aware
To read data in the standard CSV format, use:
try (CsvReader rows=new CsvReader(openReader())) {
for(CsvRecord row=rows.readNext();row!=null;row=rows.readNext()) {
// Do something here
}
}
To read data in the standard CSV format while respecting BOMs -- for example, to read CSV files exported from Excel -- use:
try (CsvReader rows=new CsvReader(Boms.decodeFromBom(openInputStream(), StandardCharsets.UTF_8))) {
for(CsvRecord row=rows.readNext();row!=null;row=rows.readNext()) {
// Do something here
}
}
To write data in the standard CSV format, use:
try (CsvWriter rows=new CsvWriter(openWriter())) {
rows.writeNext(CsvRecord.of(
CsvField.of(true, "Hello"),
CsvField.of(true, "World")));
rows.writeNext(CsvRecord.of(
CsvField.of(false, "Foo"),
CsvField.of(false, "Bar")));
}
The CsvReader also has an iterator capability:
try (CsvReader rows=new CsvReader(openReader())) {
for(CsvRecord row : rows) {
// Do something here
}
}
The CsvReader also has a stream capability:
try (CsvReader rows=new CsvReader(openReader())) {
rows.stream.forEach(r -> {
// Do something here
});
}
The csv4j library has no dependencies. However, these libraries may be useful when processing CSV data.
Users may find chardet4j useful for decoding byte streams into character streams when character encodings are not known ahead of time, for example with user input:
try (CsvReader rows=new CsvReader(Chardet.decode(openInputStream(), StandardCharsets.UTF_8))) {
// Process rows here like normal...
}
This code considers BOMs and performs a much smarter, more thorough byte frequency analysis to detect charsets.