Class CsvReader<T>
- Type Parameters:
T- The type of the object that represents one CSV row
CsvReader allows you to define how each CSV cell maps to your target object T,
and optionally integrates Bean Validation support.
Once configuration is complete, use build(InputStream) to create a CsvReadHandler.
- Since:
- 2025-07-19
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a CsvReader in setter mode without Bean Validation.Constructs a CsvReader in setter mode with instance supplier and optional validator. -
Method Summary
Modifier and TypeMethodDescriptionbuild(InputStream inputStream) Finalizes the configuration and builds aCsvReadHandlerfor parsing the given CSV stream.Sets the character encoding for reading the CSV file.column(String headerName, BiConsumer<T, CellData> setter) Registers a name-based column mapping.column(BiConsumer<T, CellData> setter) Registers a positional column mapping.columnAt(int columnIndex, BiConsumer<T, CellData> setter) Registers an index-based column mapping.delimiter(char delimiter) Sets the delimiter character used to separate fields.dialect(CsvDialect dialect) Applies a predefined CSV dialect configuration.forMap()Creates a reader that parses CSV files intoMap<String, String>rows by auto-discovering columns from the header row.Creates a reader that parses CSV files intoMap<String, String>rows, including only the specified columns.headerRowIndex(int headerRowIndex) Sets the zero-based row index of the header row.static <T> CsvReader<T> Creates a CsvReader in mapping mode for immutable object construction.static <T> CsvReader<T> Creates a CsvReader in mapping mode with Bean Validation support.onProgress(int interval, ProgressCallback callback) Registers a progress callback that fires everyintervalrows during reading.required()Marks the last registered column as required.static <T> CsvReader<T> Creates a CsvReader in setter mode.static <T> CsvReader<T> Creates a CsvReader in setter mode with Bean Validation.Skips one column during reading by registering a no-op mapping.skipColumns(int count) Skips the specified number of positional columns.
-
Constructor Details
-
CsvReader
Constructs a CsvReader in setter mode with instance supplier and optional validator.- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each rowvalidator- Optional Bean Validation validator (nullable)
-
CsvReader
Constructs a CsvReader in setter mode without Bean Validation.- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each row
-
-
Method Details
-
setter
Creates a CsvReader in setter mode. Symmetric withmapping(Function)andforMap().- Type Parameters:
T- The row data type- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each row- Returns:
- A new CsvReader configured in setter mode
- Since:
- 0.14.0
-
setter
public static <T> CsvReader<T> setter(Supplier<T> instanceSupplier, @Nullable jakarta.validation.Validator validator) Creates a CsvReader in setter mode with Bean Validation.- Type Parameters:
T- The row data type- Parameters:
instanceSupplier- A supplier to create new instances ofTfor each rowvalidator- Bean Validation validator- Returns:
- A new CsvReader configured in setter mode
- Since:
- 0.14.0
-
mapping
Creates a CsvReader in mapping mode for immutable object construction.In this mode, each row is passed as a
RowDatato the mapping function, which creates the target object in a single step.CsvReader.mapping(row -> new PersonRecord( row.get("Name").asString(), row.get("Age").asInt() )).build(inputStream).read(result -> { ... });- Type Parameters:
T- The type of the object that represents one CSV row- Parameters:
rowMapper- A function that creates an instance ofTfrom aRowData- Returns:
- A new CsvReader configured in mapping mode
-
mapping
public static <T> CsvReader<T> mapping(Function<RowData, T> rowMapper, @Nullable jakarta.validation.Validator validator) Creates a CsvReader in mapping mode with Bean Validation support.- Type Parameters:
T- The type of the object that represents one CSV row- Parameters:
rowMapper- A function that creates an instance ofTfrom aRowDatavalidator- Optional Bean Validation validator (nullable)- Returns:
- A new CsvReader configured in mapping mode
- See Also:
-
forMap
Creates a reader that parses CSV files intoMap<String, String>rows by auto-discovering columns from the header row.The returned reader exposes the standard fluent API (
dialect(CsvDialect),delimiter(char),charset(Charset),headerRowIndex(int),onProgress(int, ProgressCallback)) but rejectscolumn(BiConsumer),column(String, BiConsumer),columnAt(int, BiConsumer),skipColumn(), andskipColumns(int)at runtime — map mode infers columns automatically from the header row and does not use the setter API.CsvReader.forMap() .dialect(CsvDialect.EXCEL) .build(inputStream) .read(result -> { Map<String, String> row = result.data(); String name = row.get("Name"); });- Returns:
- a new CsvReader in map mode
- Since:
- 0.12.0
-
forMap
Creates a reader that parses CSV files intoMap<String, String>rows, including only the specified columns. Columns not listed are ignored.CsvReader.forMap("Name", "Age") .build(inputStream) .read(result -> { // result.data() contains only "Name" and "Age" keys });- Parameters:
columnNames- the header names to include (others are filtered out)- Returns:
- a new CsvReader in map mode with column filtering
- Since:
- 0.14.0
-
dialect
Applies a predefined CSV dialect configuration.Sets the delimiter and charset in one call. Individual settings can be overridden after calling this method.
- Parameters:
dialect- the dialect to apply- Returns:
- This CsvReader instance for chaining
- Since:
- 0.9.2
-
headerRowIndex
Sets the zero-based row index of the header row. Rows before this index will be skipped during reading. Defaults to 0 (the first row).- Parameters:
headerRowIndex- The zero-based index of the header row- Returns:
- This CsvReader instance for chaining
-
delimiter
Sets the delimiter character used to separate fields. Defaults to comma (',').- Parameters:
delimiter- The delimiter character- Returns:
- This CsvReader instance for chaining
-
charset
Sets the character encoding for reading the CSV file. Defaults toStandardCharsets.UTF_8.- Parameters:
charset- The charset to use- Returns:
- This CsvReader instance for chaining
-
column
Registers a positional column mapping. Columns are matched to the CSV in the order they are registered (afterheaderRowIndex(int)is accounted for).- Parameters:
setter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
column
Registers a name-based column mapping. The column is matched to the CSV column whose header equalsheaderName.- Parameters:
headerName- the header name to matchsetter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
columnAt
Registers an index-based column mapping. The column is matched to the CSV column at the given 0-based index.- Parameters:
columnIndex- 0-based column indexsetter- aBiConsumerthat writes a cell value into the row object- Returns:
- this reader for chaining
-
required
Marks the last registered column as required. A required column produces a validation error if its cell value is blank or empty.- Returns:
- this reader for chaining
- Throws:
IllegalStateException- if no columns have been registered
-
skipColumn
Skips one column during reading by registering a no-op mapping.- Returns:
- this reader for chaining
-
skipColumns
Skips the specified number of positional columns.- Parameters:
count- the number of columns to skip (must be non-negative)- Returns:
- this reader for chaining
- Throws:
IllegalArgumentException- ifcountis negative
-
onProgress
Registers a progress callback that fires everyintervalrows during reading.- Parameters:
interval- the number of rows between each callback invocation (must be positive)callback- the callback to invoke- Returns:
- This CsvReader instance for chaining
-
build
Finalizes the configuration and builds aCsvReadHandlerfor parsing the given CSV stream.- Parameters:
inputStream- The input stream of the CSV file- Returns:
- A handler to execute CSV parsing
-