CSV4J
CSV4J is a simple CSV reader and writer for Java 8 or later.
Goals
- Provide CSV reading and writing functionality compatible with both de-facto and official standards
- Expose low-level encoding data
- Simple, compact API
Non-Goals
- Object mapping
- Header handling
Why Yet Another CSV Library?
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
In response, this library is designed to be:
- Correct
- Simple
- Focused
- Transparent
Code Examples
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 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
});
}
Related projects
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...
}