Package io.github.dornol.excelkit.csv
Class CsvReadHandler<T>
java.lang.Object
io.github.dornol.excelkit.core.TempResourceContainer
io.github.dornol.excelkit.core.AbstractReadHandler<T>
io.github.dornol.excelkit.csv.CsvReadHandler<T>
- Type Parameters:
T- The target row data type
- All Implemented Interfaces:
AutoCloseable
Reads CSV files and maps rows to Java objects.
Resource management
On construction, the handler copies the input stream to a temporary file on disk. These temp resources are released when:read(java.util.function.Consumer)/AbstractReadHandler.readStrict(java.util.function.Consumer)returns or throws — cleanup is automatic.- The stream returned by
readAsStream()is closed — always use try-with-resources, since this stream also holds a background producer thread:
Abandoning the stream without closing it leaks the temp file until the JVM exits.try (Stream<ReadResult<T>> stream = handler.readAsStream()) { stream.forEach(result -> ...); }
- Since:
- 2025-07-19
-
Field Summary
Fields inherited from class io.github.dornol.excelkit.core.AbstractReadHandler
instanceSupplier, rowMapper, validator -
Method Summary
Modifier and TypeMethodDescriptionvoidread(Consumer<ReadResult<T>> consumer) Reads the file and invokes the given consumer for each row result.Reads the CSV file as a stream of row results.Methods inherited from class io.github.dornol.excelkit.core.AbstractReadHandler
mapColumn, mapColumn, mapWithRowMapper, readStrict, resolveColumnIndices, validateColumns, validateHeaderRowIndex, validateIfNeededMethods inherited from class io.github.dornol.excelkit.core.TempResourceContainer
close, getTempDir, getTempFile, setTempDir, setTempFile
-
Method Details
-
read
Description copied from class:AbstractReadHandlerReads the file and invokes the given consumer for each row result.- Specified by:
readin classAbstractReadHandler<T>- Parameters:
consumer- Callback to receive parsed and validated row results
-
readAsStream
Reads the CSV file as a stream of row results.Important: The returned stream holds file resources (CSVReader, temp file). Always use try-with-resources to ensure proper cleanup:
try (Stream<ReadResult<T>> stream = handler.readAsStream()) { stream.forEach(result -> ...); }- Specified by:
readAsStreamin classAbstractReadHandler<T>- Returns:
- A stream of parsed and validated row results
-