Reading ambiguous data
There are a few widespread ambiguities in CSV files:
- 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.
- 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.
- Extra or missing fields: CSV files can contain a different number of fields in each 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
Section titled “Example”In the following example, a CSV file with various ambiguous data is read using FastCSV.
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 foo foo,bar,baz """;
public static void main(final String[] args) { System.out.println("Reading data with default settings:"); try { CsvReader.builder() .ofCsvRecord(DATA) .forEach(System.out::println); } catch (final CsvParseException e) { System.out.println("Exception expected due to different field counts:"); e.printStackTrace(System.out); }
System.out.println("Reading data while not ignoring different field counts:"); CsvReader.builder() .allowExtraFields(true) .allowMissingFields(true) .ofCsvRecord(DATA) .forEach(System.out::println); }
}
You also find this source code example in the FastCSV GitHub repository.