Skip to content

Repository files navigation

libcsv-java

Minimal implementation of CSV parsers and formatters written in java for the following formats

Runtime requirements

  • JRE 17+

Overview

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.

Example

Parsing CSV

You can parse a CSV from a string using CsvParser#parseString(String) or a file using CsvParser#parseString(Path, Charset):

varparser = CsvParsers.ofStrictRfc4180(true);
varrecords =
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).

varparser = CsvParsers.ofStrictRfc4180(true);
varrecords = newArrayList<List<String>>();
try (varanyReader = newStringReader(
""" a,b c,d """)) {
varreader = parser.newRecordReader(anyReader);
while (reader.hasMoreRecord()) {
records.add(reader.readRecord());
}
}
// assertThat(records).containsExactly(List.of("a", "b"), List.of("c", "d"));

Formatting CSV

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):

varformatter = CsvFormatters.ofRfc4180();
varrecords = List.of(List.of("1", "2"), List.of("Hello", "World"));
varsink = 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).

varformatter = CsvFormatters.ofRfc4180();
varrecords = List.of(List.of("1", "2"), List.of("Hello", "World"));
varsink = Files.createTempFile("test", ".csv");
try (varbw = Files.newBufferedWriter(sink, StandardCharsets.UTF_8)) {
varwriter = formatter.newRecordWriter(bw);
for (varrecord : records) {
writer.writeRecord(record);
}
}
// assertThat(sink).hasContent("1,2\r\nHello,World\r\n");

References

For more details and API usage, see the javadoc in the source files and the test cases.

Parsers

Formatters

About

Minimal implimentation of CSV parser and formatter

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages