Skip to content

Reading ambiguous data

There are a few widespread ambiguities in CSV files:

  1. Empty lines: CSV files can contain empty lines, which are lines that contain no data. See the Empty lines section of the CSV Interpretation page for more information.
  2. Empty fields: CSV files can contain empty fields, which are fields that contain no data. See the Empty fields / null values section of the CSV Interpretation page for more information.
  3. Missing fields: CSV files can contain missing fields, which are fields that are not present in a record. See the Different field count of the CSV Interpretation page for more information.

FastCSV is very aware of these ambiguities and provides ways to handle them.

Many other ambiguities can occur in CSV files. The JavaCsvComparison project provides a comparison of different CSV libraries and how they handle these ambiguities.

Example

In the following example, a CSV file with various ambiguous data is read using FastCSV.

ExampleCsvReaderWithFaultyData.java
package example;
import de.siegmar.fastcsv.reader.CsvParseException;
import de.siegmar.fastcsv.reader.CsvReader;
/**
* Example for reading CSV data with faulty (or ambiguous) data.
*/
public class ExampleCsvReaderWithFaultyData {
private static final String DATA = """
foo,bar
only one field followed by some empty lines
bar,foo
""";
public static void main(final String[] args) {
System.out.println("Reading data with lenient (default) settings:");
CsvReader.builder()
.ofCsvRecord(DATA)
.forEach(System.out::println);
System.out.println("Reading data while not skipping empty lines:");
CsvReader.builder()
.skipEmptyLines(false)
.ofCsvRecord(DATA)
.forEach(System.out::println);
System.out.println("Reading data while not ignoring different field counts:");
try {
CsvReader.builder()
.ignoreDifferentFieldCount(false)
.ofCsvRecord(DATA)
.forEach(System.out::println);
} catch (final CsvParseException e) {
System.out.println(e.getMessage());
System.out.println(e.getCause().getMessage());
}
}
}

You also find this source code example in the FastCSV GitHub repository.