Class CsvBeanReader

All Implemented Interfaces:
Closeable, AutoCloseable, ICsvBeanReader, ICsvReader

public class CsvBeanReader extends AbstractCsvReader implements ICsvBeanReader
CsvBeanReader reads a CSV file by instantiating a bean for every row and mapping each column to a field on the bean (using the supplied name mapping). The bean to populate can be either a class or interface. If a class is used, it must be a valid Javabean, i.e. it must have a default no-argument constructor and getter/setter methods. An interface may also be used if it defines getters/setters - a proxy object will be created that implements the interface.
Author:
Kasper B. Graversen, James Bassett
  • Constructor Details

    • CsvBeanReader

      public CsvBeanReader(Reader reader, CsvPreference preferences)
      Constructs a new CsvBeanReader with the supplied Reader and CSV preferences. Note that the reader will be wrapped in a BufferedReader before accessed.
      Parameters:
      reader - the reader
      preferences - the CSV preferences
      Throws:
      NullPointerException - if reader or preferences are null
    • CsvBeanReader

      public CsvBeanReader(ITokenizer tokenizer, CsvPreference preferences)
      Constructs a new CsvBeanReader with the supplied (custom) Tokenizer and CSV preferences. The tokenizer should be set up with the Reader (CSV input) and CsvPreference beforehand.
      Parameters:
      tokenizer - the tokenizer
      preferences - the CSV preferences
      Throws:
      NullPointerException - if tokenizer or preferences are null
  • Method Details

    • read

      public <T> T read(Class<T> clazz, String... nameMapping) throws IOException
      Reads a row of a CSV file and populates an instance of the specified class, using the supplied name mapping to map column values to the appropriate fields.
      Specified by:
      read in interface ICsvBeanReader
      Type Parameters:
      T - the bean type
      Parameters:
      clazz - the type to instantiate. If the type is a class then a new instance will be created using the default no-args constructor. If the type is an interface, a proxy object which implements the interface will be created instead.
      nameMapping - an array of Strings linking the CSV columns to their corresponding field in the bean (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the field in the bean will be null - or its default value).
      Returns:
      a populated bean or null if EOF
      Throws:
      IOException - if an I/O error occurred
    • read

      public <T> T read(Class<T> clazz, String[] nameMapping, CellProcessor... processors) throws IOException
      Reads a row of a CSV file and populates an instance of the specified class, using the supplied name mapping to map column values to the appropriate fields. Before population the data can be further processed by cell processors (as with the nameMapping array, each element in the processors array corresponds with a CSV column). A null entry in the processors array indicates no further processing is required (the unprocessed String value will be set on the bean's field).
      Specified by:
      read in interface ICsvBeanReader
      Type Parameters:
      T - the bean type
      Parameters:
      clazz - the type to instantiate. If the type is a class then a new instance will be created using the default no-args constructor. If the type is an interface, a proxy object which implements the interface will be created instead.
      nameMapping - an array of Strings linking the CSV columns to their corresponding field in the bean (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the field in the bean will be null - or its default value).
      processors - an array of CellProcessors used to further process data before it is populated on the bean (each element in the processors array corresponds with a CSV column - the number of processors should match the number of columns). A null entry indicates no further processing is required (the unprocessed String value will be set on the bean's field).
      Returns:
      a populated bean or null if EOF
      Throws:
      IOException - if an I/O error occurred
    • read

      public <T> T read(T bean, String... nameMapping) throws IOException
      Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the appropriate fields.
      Specified by:
      read in interface ICsvBeanReader
      Type Parameters:
      T - the bean type
      Parameters:
      bean - the bean to populate
      nameMapping - an array of Strings linking the CSV columns to their corresponding field in the bean (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the field in the bean will be null - or its default value).
      Returns:
      a populated bean or null if EOF
      Throws:
      IOException - if an I/O error occurred
    • read

      public <T> T read(T bean, String[] nameMapping, CellProcessor... processors) throws IOException
      Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the appropriate fields. Before population the data can be further processed by cell processors (as with the nameMapping array, each element in the processors array corresponds with a CSV column). A null entry in the processors array indicates no further processing is required (the unprocessed String value will be set on the bean's field).
      Specified by:
      read in interface ICsvBeanReader
      Type Parameters:
      T - the bean type
      Parameters:
      bean - the bean to populate
      nameMapping - an array of Strings linking the CSV columns to their corresponding field in the bean (the array length should match the number of columns). A null entry in the array indicates that the column should be ignored (the field in the bean will be null - or its default value).
      processors - an array of CellProcessors used to further process data before it is populated on the bean (each element in the processors array corresponds with a CSV column - the number of processors should match the number of columns). A null entry indicates no further processing is required (the unprocessed String value will be set on the bean's field).
      Returns:
      a populated bean or null if EOF
      Throws:
      IOException - if an I/O error occurred