Minimal implementation of CSV parsers and formatters written in java for the following formats
- JRE 17+
The central APIs of parsing and formatting CSVs are CsvParser class and CsvFormatter class.
Parsers only unmarshal a CSV record into List<String> and formatters only marshal List<String> into a CSV record for now.
See the javadoc for full API documentation.
You can parse a CSV from a string using CsvParser#parseString(String) or a file using CsvParser#parseString(Path, Charset):
var parser = CsvParsers.ofStrictRfc4180(true);
var records =
parser.parseString(
"""
circle,"r=5,0"
square,"d=2,0"
""");
// assertThat(records).containsExactly(List.of("circle", "r=5,0"), List.of("square", "d=2,0"));For large files or streaming, use RecordReader which allows you to write an interator-like way to access a record at a time.
RecordReader can be created by CsvParser#newRecordReader(Reader).
var parser = CsvParsers.ofStrictRfc4180(true);
var records = new ArrayList<List<String>>();
try (var anyReader = new StringReader(
"""
a,b
c,d
""")) {
var reader = parser.newRecordReader(anyReader);
while (reader.hasMoreRecord()) {
records.add(reader.readRecord());
}
}
// assertThat(records).containsExactly(List.of("a", "b"), List.of("c", "d"));You can format records into a CSV from a string using CsvFormatter#formatToString(List<String>) or a file using CsvFormatter#formatToFile(List<String>, Path, Charset):
var formatter = CsvFormatters.ofRfc4180();
var records = List.of(List.of("1", "2"), List.of("Hello", "World"));
var sink = Files.createTempFile("test", ".csv");
formatter.formatToFile(records, sink, StandardCharsets.UTF_8);
// assertThat(sink).hasContent("1,2\r\nHello,World\r\n");For large number of records or streaming, use RecordWriter which allows you to write an interator-like way to format a record at a time.
RecordWriter can be created by CsvFormatter#newRecordWriter(Writer).
var formatter = CsvFormatters.ofRfc4180();
var records = List.of(List.of("1", "2"), List.of("Hello", "World"));
var sink = Files.createTempFile("test", ".csv");
try (var bw = Files.newBufferedWriter(sink, StandardCharsets.UTF_8)) {
var writer = formatter.newRecordWriter(bw);
for (var record : records) {
writer.writeRecord(record);
}
}
// assertThat(sink).hasContent("1,2\r\nHello,World\r\n");For more details and API usage, see the javadoc in the source files and the test cases.