Top Description Fields Constructors Methods
com.opencsv.bean

public Class CsvToBeanBuilder<T>

extends Object
Class Inheritance
Type Parameters
<T>
Type of the bean to be populated
Imports
com.opencsv.*, com.opencsv.bean.exceptionhandler.CsvExceptionHandler, .ExceptionHandlerThrow, com.opencsv.bean.util.OpencsvUtils, com.opencsv.enums.CSVReaderNullFieldIndicator, org.apache.commons.collections4.ListValuedMap, .MultiValuedMap, org.apache.commons.collections4.multimap.ArrayListValuedHashMap, org.apache.commons.lang3.ObjectUtils, .StringUtils, java.io.Reader, java.lang.reflect.Field, java.util.LinkedList, .List, .Locale, .ResourceBundle

This class makes it possible to bypass all the intermediate steps and classes in setting up to read from a CSV source to a list of beans.

This is the place to start if you're reading a CSV source into beans, especially if you're binding the input's columns to the bean's variables using the annotations CsvBindByName, CsvCustomBindByName, CsvBindByPosition, or CsvCustomBindByPosition.

If you want nothing but defaults for the entire import, your code can look as simple as this, where myreader is any valid Reader:
List<MyBean> result = new CsvToBeanBuilder(myreader).withType(MyBean.class).build().parse();

This builder is intelligent enough to guess the mapping strategy according to the following strategy:

  1. If a mapping strategy is explicitly set, it is always used.
  2. If CsvBindByPosition or CsvCustomBindByPosition is present, ColumnPositionMappingStrategy is used.
  3. Otherwise, HeaderColumnNameMappingStrategy is used. This includes the case when CsvBindByName or CsvCustomBindByName are being used. The annotations will automatically be recognized.
Author
Andrew Rucker Jones
Since
3.9

Field Summary

Modifier and TypeField and Description
private final CSVReader
csvReader

Allow the user to pass in a prebuilt/custom com.opencsv.CSVReader.

private Locale
private Character
private CsvExceptionHandler
private CsvToBeanFilter
private final ListValuedMap<Class<?>, Field>
private boolean
private Boolean
private Boolean
private boolean
private MappingStrategy<? extends T>
private Integer
private CSVReaderNullFieldIndicator
private boolean
private String
private Character
private final Reader
reader

A CSVReader will be built out of this java.io.Reader.

private Character
private Integer
private Boolean
private Boolean
private Class<? extends T>
private final List<BeanVerifier<T>>
private Boolean

Constructor Summary

AccessConstructor and Description
public
CsvToBeanBuilder(Reader
The reader that is the source of data for the CSV import
reader
)

Constructor with the one parameter that is most definitely mandatory, and always will be.

public
CsvToBeanBuilder(CSVReader
The CSVReader that is the source of data for the CSV import
csvReader
)

Constructor with the one parameter that is most definitely mandatory, and always will be.

Method Summary

Modifier and TypeMethod and Description
public CsvToBean<T>

Returns:

A valid CsvToBean
build
()

Builds the CsvToBean out of the provided information.

private CSVParser

Returns:

An appropriate CSVParser
buildParser
()

Builds a CSVParser from the information provided to this builder.

private CSVReader

Returns:

An appropriate CSVReader
buildReader
(CSVParser
The CSVParser necessary for this reader
parser
)

Builds a CSVReader from the information provided to this builder.

public CsvToBeanBuilder<T>

Returns:

this
withErrorLocale
(Locale
Locale for error messages
errorLocale
)

Sets the locale for all error messages.

public CsvToBeanBuilder<T>
withEscapeChar(char
Please see the "See Also" section
escapeChar
)

Returns: this

public CsvToBeanBuilder<T>

Returns:

this
withExceptionHandler
(CsvExceptionHandler
The exception handler to be used. If null, this method does nothing.
exceptionHandler
)

Sets the handler for recoverable exceptions raised during processing of records.

public CsvToBeanBuilder<T>
withFieldAsNull(CSVReaderNullFieldIndicator
Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)
indicator
)

Returns: this

public CsvToBeanBuilder<T>
withFilter(CsvToBeanFilter
Please see the "See Also" section
filter
)

Returns: this

public CsvToBeanBuilder<T>
withIgnoreEmptyLine(boolean
Please see the "See Also" section
ignore
)

Returns: this

public CsvToBeanBuilder<T>

Returns:

this
withIgnoreField
(Class<?>
The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.
type
,
Field
The field opencsv is to ignore
field
)

Adds a Field to the list of fields opencsv should ignore completely.

public CsvToBeanBuilder<T>
withIgnoreLeadingWhiteSpace(boolean
Please see the "See Also" section
ignoreLeadingWhiteSpace
)

Returns: this

public CsvToBeanBuilder<T>
withIgnoreQuotations(boolean
Please see the "See Also" section
ignoreQuotations
)

Returns: this

public CsvToBeanBuilder<T>
withKeepCarriageReturn(boolean
True to keep carriage returns in data read, false otherwise
keepCR
)

Returns: this

public CsvToBeanBuilder<T>
withMappingStrategy(MappingStrategy<? extends T>
Please see the "See Also" section
mappingStrategy
)

Returns: this

public CsvToBeanBuilder<T>

Returns:

this
withMultilineLimit
(int
No more than this number of lines is allowed in a single input record. The default is CSVReader#DEFAULT_MULTILINE_LIMIT.
multilineLimit
)

Sets the maximum number of lines allowed in a multiline record.

public CsvToBeanBuilder<T>

Returns:

this
withOrderedResults
(boolean
Whether to order the results or not
orderedResults
)

Sets whether the resulting beans must be ordered as in the input.

public CsvToBeanBuilder<T>

Returns:

this
withProfile
(String
The name of the profile to be used
profile
)

Selects a profile for deciding which configurations to use for the bean fields.

public CsvToBeanBuilder<T>
withQuoteChar(char
Please see the "See Also" section
quoteChar
)

Returns: this

public CsvToBeanBuilder<T>
withSeparator(char
Please see the "See Also" section
separator
)

Returns: this

public CsvToBeanBuilder<T>
withSkipLines(final int
Please see the "See Also" section
skipLines
)

Returns: this

public CsvToBeanBuilder<T>
withStrictQuotes(boolean
Please see the "See Also" section
strictQuotes
)

Returns: this

public CsvToBeanBuilder<T>

Returns:

this
withThrowExceptions
(boolean
Please see the "See Also" section
throwExceptions
)

Sets how the CsvToBean will act when an exception occurs.

public CsvToBeanBuilder<T>

Returns:

this
withType
(Class<? extends T>
Class of the destination bean
type
)

Sets the type of the bean to be populated.

public CsvToBeanBuilder<T>

Returns:

this
withVerifier
(BeanVerifier<T>
A new verifier that is to process all beans after creation. null is permissible but has no effect.
verifier
)

Adds a BeanVerifier to the list of verifiers to run on all beans created.

public CsvToBeanBuilder<T>
withVerifyReader(boolean
Please see the "See Also" section
verifyReader
)

Returns: this

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

csvReaderback to summary
private final CSVReader csvReader

Allow the user to pass in a prebuilt/custom com.opencsv.CSVReader.

errorLocaleback to summary
private Locale errorLocale
See Also
com.opencsv.bean.CsvToBean#errorLocale
escapeCharback to summary
private Character escapeChar
See Also
com.opencsv.CSVParser#escape
exceptionHandlerback to summary
private CsvExceptionHandler exceptionHandler
See Also
com.opencsv.bean.CsvToBean#setThrowExceptions(boolean)
filterback to summary
private CsvToBeanFilter filter
See Also
com.opencsv.bean.CsvToBean#filter
ignoredFieldsback to summary
private final ListValuedMap<Class<?>, Field> ignoredFields
See Also
com.opencsv.bean.AbstractMappingStrategy#ignoredFields
ignoreEmptyLinesback to summary
private boolean ignoreEmptyLines
See Also
com.opencsv.bean.CsvToBean#ignoreEmptyLines
ignoreLeadingWhiteSpaceback to summary
private Boolean ignoreLeadingWhiteSpace
See Also
com.opencsv.CSVParser#ignoreLeadingWhiteSpace
ignoreQuotationsback to summary
private Boolean ignoreQuotations
See Also
com.opencsv.CSVParser#ignoreQuotations
keepCRback to summary
private boolean keepCR
See Also
com.opencsv.CSVReader#keepCR
mappingStrategyback to summary
private MappingStrategy<? extends T> mappingStrategy
See Also
com.opencsv.bean.CsvToBean#mappingStrategy
multilineLimitback to summary
private Integer multilineLimit
See Also
com.opencsv.CSVReader#multilineLimit
nullFieldIndicatorback to summary
private CSVReaderNullFieldIndicator nullFieldIndicator
See Also
com.opencsv.CSVParser#nullFieldIndicator
orderedResultsback to summary
private boolean orderedResults
See Also
com.opencsv.bean.CsvToBean#orderedResults
profileback to summary
private String profile
See Also
com.opencsv.bean.AbstractMappingStrategy#profile
quoteCharback to summary
private Character quoteChar
See Also
com.opencsv.CSVParser#quotechar
readerback to summary
private final Reader reader

A CSVReader will be built out of this java.io.Reader.

See Also
com.opencsv.bean.CsvToBean#csvReader
separatorback to summary
private Character separator
See Also
com.opencsv.CSVParser#separator
skipLinesback to summary
private Integer skipLines
See Also
com.opencsv.CSVReader#skipLines
strictQuotesback to summary
private Boolean strictQuotes
See Also
com.opencsv.CSVParser#strictQuotes
throwsExceptionsback to summary
private Boolean throwsExceptions
See Also
com.opencsv.bean.CsvToBean#setThrowExceptions(boolean)
typeback to summary
private Class<? extends T> type
See Also
HeaderColumnNameMappingStrategy#type
verifiersback to summary
private final List<BeanVerifier<T>> verifiers
See Also
com.opencsv.bean.CsvToBean#verifiers
verifyReaderback to summary
private Boolean verifyReader
See Also
com.opencsv.CSVReader#verifyReader

Constructor Detail

CsvToBeanBuilderback to summary
public CsvToBeanBuilder(Reader reader)

Constructor with the one parameter that is most definitely mandatory, and always will be.

Parameters
reader:Reader

The reader that is the source of data for the CSV import

CsvToBeanBuilderback to summary
public CsvToBeanBuilder(CSVReader csvReader)

Constructor with the one parameter that is most definitely mandatory, and always will be.

Parameters
csvReader:CSVReader

The CSVReader that is the source of data for the CSV import

Method Detail

buildback to summary
public CsvToBean<T> build() throws IllegalStateException

Builds the CsvToBean out of the provided information.

Returns:CsvToBean<T>

A valid CsvToBean

Exceptions
IllegalStateException:
If a necessary parameter was not specified. Currently this means that both the mapping strategy and the bean type are not set, so it is impossible to determine a mapping strategy.
buildParserback to summary
private CSVParser buildParser()

Builds a CSVParser from the information provided to this builder. This is an intermediate step in building the CsvToBean.

Returns:CSVParser

An appropriate CSVParser

buildReaderback to summary
private CSVReader buildReader(CSVParser parser)

Builds a CSVReader from the information provided to this builder. This is an intermediate step in building the CsvToBean.

Parameters
parser:CSVParser

The CSVParser necessary for this reader

Returns:CSVReader

An appropriate CSVReader

withErrorLocaleback to summary
public CsvToBeanBuilder<T> withErrorLocale(Locale errorLocale)

Sets the locale for all error messages.

Parameters
errorLocale:Locale

Locale for error messages

Returns:CsvToBeanBuilder<T>

this

Since
4.0
See Also
CsvToBean#setErrorLocale(java.util.Locale)
withEscapeCharback to summary
public CsvToBeanBuilder<T> withEscapeChar(char escapeChar)
Parameters
escapeChar:char

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withExceptionHandlerback to summary
public CsvToBeanBuilder<T> withExceptionHandler(CsvExceptionHandler exceptionHandler)

Sets the handler for recoverable exceptions raised during processing of records. If both withThrowExceptions(boolean) and withExceptionHandler are used then the withExceptionHandler takes precedence and is used.

If neither this method nor withThrowExceptions(boolean) is called, the default exception handler is ExceptionHandlerThrow.

Please note that if both this method and withThrowExceptions(boolean) are called, the last call wins.

Parameters
exceptionHandler:CsvExceptionHandler

The exception handler to be used. If null, this method does nothing.

Returns:CsvToBeanBuilder<T>

this

Since
5.2
withFieldAsNullback to summary
public CsvToBeanBuilder<T> withFieldAsNull(CSVReaderNullFieldIndicator indicator)
Parameters
indicator:CSVReaderNullFieldIndicator

Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)

Returns:CsvToBeanBuilder<T>

this

withFilterback to summary
public CsvToBeanBuilder<T> withFilter(CsvToBeanFilter filter)
Parameters
filter:CsvToBeanFilter

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CsvToBean#setFilter(com.opencsv.bean.CsvToBeanFilter)
withIgnoreEmptyLineback to summary
public CsvToBeanBuilder<T> withIgnoreEmptyLine(boolean ignore)
Parameters
ignore:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CsvToBean#ignoreEmptyLines
withIgnoreFieldback to summary
public CsvToBeanBuilder<T> withIgnoreField(Class<?> type, Field field) throws IllegalArgumentException

Adds a Field to the list of fields opencsv should ignore completely.

May be called as many times as necessary.

Parameters
type:Class<?>

The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.

field:Field

The field opencsv is to ignore

Returns:CsvToBeanBuilder<T>

this

Exceptions
IllegalArgumentException:
If one of the parameters is null or field cannot be found in type.
Since
5.0
See Also
MappingStrategy#ignoreFields(MultiValuedMap)
withIgnoreLeadingWhiteSpaceback to summary
public CsvToBeanBuilder<T> withIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace)
Parameters
ignoreLeadingWhiteSpace:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withIgnoreQuotationsback to summary
public CsvToBeanBuilder<T> withIgnoreQuotations(boolean ignoreQuotations)
Parameters
ignoreQuotations:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withKeepCarriageReturnback to summary
public CsvToBeanBuilder<T> withKeepCarriageReturn(boolean keepCR)
Parameters
keepCR:boolean

True to keep carriage returns in data read, false otherwise

Returns:CsvToBeanBuilder<T>

this

withMappingStrategyback to summary
public CsvToBeanBuilder<T> withMappingStrategy(MappingStrategy<? extends T> mappingStrategy)
Parameters
mappingStrategy:MappingStrategy<? extends T>

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CsvToBean#setMappingStrategy(com.opencsv.bean.MappingStrategy)
withMultilineLimitback to summary
public CsvToBeanBuilder<T> withMultilineLimit(int multilineLimit)

Sets the maximum number of lines allowed in a multiline record. More than this number in one record results in an IOException.

Parameters
multilineLimit:int

No more than this number of lines is allowed in a single input record. The default is CSVReader#DEFAULT_MULTILINE_LIMIT.

Returns:CsvToBeanBuilder<T>

this

withOrderedResultsback to summary
public CsvToBeanBuilder<T> withOrderedResults(boolean orderedResults)

Sets whether the resulting beans must be ordered as in the input.

Parameters
orderedResults:boolean

Whether to order the results or not

Returns:CsvToBeanBuilder<T>

this

Since
4.0
See Also
CsvToBean#setOrderedResults(boolean)
withProfileback to summary
public CsvToBeanBuilder<T> withProfile(String profile)

Selects a profile for deciding which configurations to use for the bean fields.

Parameters
profile:String

The name of the profile to be used

Returns:CsvToBeanBuilder<T>

this

Since
5.4
withQuoteCharback to summary
public CsvToBeanBuilder<T> withQuoteChar(char quoteChar)
Parameters
quoteChar:char

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withSeparatorback to summary
public CsvToBeanBuilder<T> withSeparator(char separator)
Parameters
separator:char

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withSkipLinesback to summary
public CsvToBeanBuilder<T> withSkipLines(final int skipLines)
Parameters
skipLines:int

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVReaderBuilder#withSkipLines(int)
withStrictQuotesback to summary
public CsvToBeanBuilder<T> withStrictQuotes(boolean strictQuotes)
Parameters
strictQuotes:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVParser#CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
withThrowExceptionsback to summary
public CsvToBeanBuilder<T> withThrowExceptions(boolean throwExceptions)

Sets how the CsvToBean will act when an exception occurs. If both withThrowsExcpetion and withExceptionHandler(CsvExceptionHandler) are used then the withExceptionHandler takes precedence and is used.

Parameters
throwExceptions:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CsvToBean#setThrowExceptions(boolean), withExceptionHandler(CsvExceptionHandler)
withTypeback to summary
public CsvToBeanBuilder<T> withType(Class<? extends T> type)

Sets the type of the bean to be populated. Ignored if withMappingStrategy(com.opencsv.bean.MappingStrategy) is called.

Parameters
type:Class<? extends T>

Class of the destination bean

Returns:CsvToBeanBuilder<T>

this

See Also
HeaderColumnNameMappingStrategy#setType(java.lang.Class), ColumnPositionMappingStrategy#setType(java.lang.Class)
withVerifierback to summary
public CsvToBeanBuilder<T> withVerifier(BeanVerifier<T> verifier)

Adds a BeanVerifier to the list of verifiers to run on all beans created. This method may be called as many times as desired. All added verifiers will be run on every bean. No guarantee is made as to the order in which the verifiers are run.

Parameters
verifier:BeanVerifier<T>

A new verifier that is to process all beans after creation. null is permissible but has no effect.

Returns:CsvToBeanBuilder<T>

this

Since
4.4
withVerifyReaderback to summary
public CsvToBeanBuilder<T> withVerifyReader(boolean verifyReader)
Parameters
verifyReader:boolean

Please see the "See Also" section

Returns:CsvToBeanBuilder<T>

this

See Also
CSVReaderBuilder#withVerifyReader(boolean)