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>

public class CsvReader<T> extends Object implements Iterable<T>
A generic CSV file reader using Jackson CSV module. Supports both header-based and headerless CSV files.
  • Field Details

  • Constructor Details

    • CsvReader

      public CsvReader(Path path, Class<T> clazz) throws Exception
      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 file
      clazz - the class to map CSV rows to
      Throws:
      Exception - if an error occurs during reading
    • CsvReader

      public CsvReader(InputStream inputStream, Class<T> clazz, Properties properties) throws Exception
      Constructs a CsvReader using an InputStream, target class, and optional configuration.
      Parameters:
      inputStream - the input stream of the CSV file
      clazz - the class to map CSV rows to
      properties - 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

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • 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 row
      columnSeparator - the delimiter used in the CSV
      schemaDefinition - the comma-separated list of column names (used if no header)
      Returns:
      the constructed CsvSchema