Reading as String Arrays
The typical way to read CSV data with FastCSV is to use one of the CsvReaderBuilder methods ofCsvRecord() or ofNamedCsvRecord().
These methods provide convenient access to CSV data as a stream of CsvRecord or NamedCsvRecord objects.
Under the hood, FastCSV uses a callback handler to process CSV data and create the objects that represent each CSV record.
The callback handlers CsvRecordHandler and NamedCsvRecordHandler back the convenience methods ofCsvRecord() and ofNamedCsvRecord().
Although CsvRecord and NamedCsvRecord are convenient, they introduce some overhead because they are designed as immutable objects that
encapsulate the read CSV data and provide access only through unmodifiable lists or maps.
This overhead is usually negligible, but if object allocation becomes a bottleneck, and you prefer not to implement a
custom callback handler, FastCSV offers the built-in StringArrayHandler.
Example
Section titled “Example”The following example demonstrates how to read CSV data as string arrays.
import java.io.IOException;
import de.siegmar.fastcsv.reader.CsvReader;import de.siegmar.fastcsv.reader.StringArrayHandler;
/// Example for reading CSV with direct String array access.////// String array access addresses the edge case where the number of allocated/// objects needs to be minimized, e.g., in high-performance scenarios.////// **ONLY use it if you are absolutely sure that you need it!**////// FastCSV supports Java 17 and later, but this code uses Java 25/// for brevity, leveraging newer language features.void main() throws IOException { final String data = """ jane,doe john,smith """;
final var handler = StringArrayHandler.builder().build();
try (CsvReader<String[]> csv = CsvReader.builder().build(handler, data)) { for (final String[] fields : csv) { IO.println("First Name: " + fields[0] + ", Last Name: " + fields[1]); } }}You also find this source code example in the FastCSV GitHub repository.