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.
  • Field Details

  • 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

    • instantiateBean

      private static <T> T instantiateBean(Class<T> clazz)
      Instantiates the bean (or creates a proxy if it's an interface).
      Parameters:
      clazz - the bean class to instantiate (a proxy will be created if an interface is supplied), using the default (no argument) constructor
      Returns:
      the instantiated bean
      Throws:
      SuperCsvReflectionException - if there was a reflection exception when instantiating the bean
    • invokeSetter

      private static void invokeSetter(Object bean, Method setMethod, Object fieldValue)
      Invokes the setter on the bean with the supplied value.
      Parameters:
      bean - the bean
      setMethod - the setter method for the field
      fieldValue - the field value to set
      Throws:
      SuperCsvException - if there was an exception invoking the setter
    • populateBean

      private <T> T populateBean(T resultBean, String[] nameMapping)
      Populates the bean by mapping the processed columns to the fields of the bean.
      Parameters:
      resultBean - the bean to populate
      nameMapping - the name mappings
      Returns:
      the populated bean
      Throws:
      SuperCsvReflectionException - if there was a reflection exception while populating the bean
    • 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
    • readIntoBean

      private <T> T readIntoBean(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. If processors are supplied then they are used, otherwise the raw String values will be used.
      Parameters:
      bean - the bean to populate
      nameMapping - the name mapping array
      processors - the (optional) cell processors
      Returns:
      the populated bean, or null if EOF was reached
      Throws:
      IllegalArgumentException - if nameMapping.length != number of CSV columns read
      IOException - if an I/O error occurred
      NullPointerException - if bean or nameMapping are null
      SuperCsvConstraintViolationException - if a CellProcessor constraint failed
      SuperCsvException - if there was a general exception while reading/processing
      SuperCsvReflectionException - if there was an reflection exception while mapping the values to the bean