Skip to content

Migrating from 1.x

This document only describes the breaking changes when migrating from FastCSV 1.x to 3.x. For a full list of changes, including new features, see the changelog.

Reader

Configuring the reader

Old way:

CsvReader csvReader = new CsvReader();
csvReader.setFieldSeparator(',');
csvReader.setTextDelimiter('"');
csvReader.setSkipEmptyRows(true);
csvReader.setErrorOnDifferentFieldCount(false);

New way:

CsvReader.builder()
.fieldSeparator(',')
.quoteCharacter('"')
.skipEmptyLines(true)
.ignoreDifferentFieldCount(true);

Reading data from file

Old way:

try (CsvParser csvParser = new CsvReader().parse(file, UTF_8)) {
CsvRow row;
while ((row = csvParser.nextRow()) != null) {
System.out.println("First field of row: " + row.getField(0));
}
}

New way:

try (CsvReader<CsvRecord> csv = CsvReader.builder().ofCsvRecord(file)) {
csv.forEach(rec ->
System.out.println("First field of record: " + rec.getField(0))
);
}

Reading data with a header from file

Old way:

CsvReader csvReader = new CsvReader();
csvReader.setContainsHeader(true);
try (CsvParser csvParser = csvReader.parse(file, UTF_8)) {
CsvRow row;
while ((row = csvParser.nextRow()) != null) {
System.out.println("Field named firstname: " + row.getField("firstname"));
}
}

New way:

try (CsvReader<NamedCsvRecord> csv = CsvReader.builder().ofNamedCsvRecord(file)) {
csv.forEach(rec ->
System.out.println("Field named firstname: " + rec.getField("firstname"))
);
}

Read an entire file at once

Old way:

CsvContainer csv = new CsvReader().read(file, UTF_8);

New way:

The container concept has been removed, but you can easily build something similar using the Java Stream API.

List<CsvRecord> data;
try (CsvReader<CsvRecord> csvReader = CsvReader.builder().ofCsvRecord(file)) {
data = csvReader.stream().toList();
}

Writer

Configuring the writer

Old way:

CsvWriter csvWriter = new CsvWriter();
csvWriter.setFieldSeparator(',');
csvWriter.setTextDelimiter('"');
csvWriter.setAlwaysDelimitText(true);
csvWriter.setLineDelimiter(new char[]{'\r','\n'});

New way:

CsvWriter.builder()
.fieldSeparator(',')
.quoteCharacter('"')
.quoteStrategy(QuoteStrategies.ALWAYS)
.lineDelimiter(LineDelimiter.CRLF);

Write to file

Old way:

try (CsvAppender csvAppender = new CsvWriter().append(file)) {
csvAppender.appendLine("header1", "header2");
csvAppender.appendLine("value1", "value2");
}

New way:

try (CsvWriter csvWriter = CsvWriter.builder().build(file)) {
csvWriter
.writeRecord("header1", "header2")
.writeRecord("value1", "value2");
}

Write to writer

Old way:

Writer writer = new StringWriter();
try (CsvAppender csvAppender = new CsvWriter().append(writer)) {
csvAppender.appendLine("header1", "header2");
csvAppender.appendLine("value1", "value2");
}

New way:

Writer writer = new StringWriter();
try (CsvWriter csvWriter = CsvWriter.builder().build(writer)) {
csvWriter
.writeRecord("header1", "header2")
.writeRecord("value1", "value2");
}