Package org.supercsv.io
Class CsvBeanReader
- java.lang.Object
-
- org.supercsv.io.AbstractCsvReader
-
- org.supercsv.io.CsvBeanReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.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 Summary
Fields Modifier and Type Field Description private MethodCache
cache
private java.util.List<java.lang.Object>
processedColumns
-
Constructor Summary
Constructors Constructor Description CsvBeanReader(java.io.Reader reader, CsvPreference preferences)
Constructs a new CsvBeanReader with the supplied Reader and CSV preferences.CsvBeanReader(ITokenizer tokenizer, CsvPreference preferences)
Constructs a new CsvBeanReader with the supplied (custom) Tokenizer and CSV preferences.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static <T> T
instantiateBean(java.lang.Class<T> clazz)
Instantiates the bean (or creates a proxy if it's an interface).private static void
invokeSetter(java.lang.Object bean, java.lang.reflect.Method setMethod, java.lang.Object fieldValue)
Invokes the setter on the bean with the supplied value.private <T> T
populateBean(T resultBean, java.lang.String[] nameMapping)
Populates the bean by mapping the processed columns to the fields of the bean.<T> T
read(java.lang.Class<T> clazz, java.lang.String... nameMapping)
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.<T> T
read(java.lang.Class<T> clazz, java.lang.String[] nameMapping, CellProcessor... processors)
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.<T> T
read(T bean, java.lang.String... nameMapping)
Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the appropriate fields.<T> T
read(T bean, java.lang.String[] nameMapping, CellProcessor... processors)
Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the appropriate fields.private <T> T
readIntoBean(T bean, java.lang.String[] nameMapping, CellProcessor[] processors)
Reads a row of a CSV file and populates the bean, using the supplied name mapping to map column values to the appropriate fields.-
Methods inherited from class org.supercsv.io.AbstractCsvReader
close, executeProcessors, get, getColumns, getHeader, getLineNumber, getPreferences, getRowNumber, getUntokenizedRow, length, readRow
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.supercsv.io.ICsvReader
get, getHeader, getLineNumber, getRowNumber, getUntokenizedRow, length
-
-
-
-
Field Detail
-
processedColumns
private final java.util.List<java.lang.Object> processedColumns
-
cache
private final MethodCache cache
-
-
Constructor Detail
-
CsvBeanReader
public CsvBeanReader(java.io.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 readerpreferences
- the CSV preferences- Throws:
java.lang.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 tokenizerpreferences
- the CSV preferences- Throws:
java.lang.NullPointerException
- if tokenizer or preferences are null
-
-
Method Detail
-
instantiateBean
private static <T> T instantiateBean(java.lang.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(java.lang.Object bean, java.lang.reflect.Method setMethod, java.lang.Object fieldValue)
Invokes the setter on the bean with the supplied value.- Parameters:
bean
- the beansetMethod
- the setter method for the fieldfieldValue
- the field value to set- Throws:
SuperCsvException
- if there was an exception invoking the setter
-
populateBean
private <T> T populateBean(T resultBean, java.lang.String[] nameMapping)
Populates the bean by mapping the processed columns to the fields of the bean.- Parameters:
resultBean
- the bean to populatenameMapping
- the name mappings- Returns:
- the populated bean
- Throws:
SuperCsvReflectionException
- if there was a reflection exception while populating the bean
-
read
public <T> T read(java.lang.Class<T> clazz, java.lang.String... nameMapping) throws java.io.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 interfaceICsvBeanReader
- 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:
java.io.IOException
- if an I/O error occurred
-
read
public <T> T read(java.lang.Class<T> clazz, java.lang.String[] nameMapping, CellProcessor... processors) throws java.io.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 interfaceICsvBeanReader
- 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:
java.io.IOException
- if an I/O error occurred
-
read
public <T> T read(T bean, java.lang.String... nameMapping) throws java.io.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 interfaceICsvBeanReader
- Type Parameters:
T
- the bean type- Parameters:
bean
- the bean to populatenameMapping
- 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:
java.io.IOException
- if an I/O error occurred
-
read
public <T> T read(T bean, java.lang.String[] nameMapping, CellProcessor... processors) throws java.io.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 interfaceICsvBeanReader
- Type Parameters:
T
- the bean type- Parameters:
bean
- the bean to populatenameMapping
- 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:
java.io.IOException
- if an I/O error occurred
-
readIntoBean
private <T> T readIntoBean(T bean, java.lang.String[] nameMapping, CellProcessor[] processors) throws java.io.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 populatenameMapping
- the name mapping arrayprocessors
- the (optional) cell processors- Returns:
- the populated bean, or null if EOF was reached
- Throws:
java.lang.IllegalArgumentException
- if nameMapping.length != number of CSV columns readjava.io.IOException
- if an I/O error occurredjava.lang.NullPointerException
- if bean or nameMapping are nullSuperCsvConstraintViolationException
- if a CellProcessor constraint failedSuperCsvException
- if there was a general exception while reading/processingSuperCsvReflectionException
- if there was an reflection exception while mapping the values to the bean
-
-