Package org.chenile.utils.stream
Class CsvReader<T>
java.lang.Object
org.chenile.utils.stream.CsvReader<T>
- Type Parameters:
T
- the target type to map each CSV row to
- All Implemented Interfaces:
Iterable<T>
A generic CSV file reader using Jackson CSV module.
Supports both header-based and headerless CSV files.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCsvReader
(InputStream inputStream, Class<T> clazz, Properties properties) Constructs a CsvReader using an InputStream, target class, and optional configuration.Constructs a CsvReader using a file path and target class. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
mappingIterator
-
-
Constructor Details
-
CsvReader
Constructs a CsvReader using a file path and target class. Assumes the CSV has headers and comma as the default separator.- Parameters:
path
- the path to the CSV fileclazz
- the class to map CSV rows to- Throws:
Exception
- if an error occurs during reading
-
CsvReader
Constructs a CsvReader using an InputStream, target class, and optional configuration.- Parameters:
inputStream
- the input stream of the CSV fileclazz
- the class to map CSV rows toproperties
- configuration for CSV parsing: - csv.columnSeparator (default: ",") - csv.hasHeader (default: true) - csv.schema (comma-separated field names; required if no header)- Throws:
Exception
- if parsing fails or required configuration is missing
-
-
Method Details
-
iterator
-
buildSchema
private com.fasterxml.jackson.dataformat.csv.CsvSchema buildSchema(boolean hasHeader, String columnSeparator, String schemaDefinition) Builds a CSV schema either with headers or with explicitly defined column names.- Parameters:
hasHeader
- whether the CSV contains a header rowcolumnSeparator
- the delimiter used in the CSVschemaDefinition
- the comma-separated list of column names (used if no header)- Returns:
- the constructed CsvSchema
-