Class CsvToBeanBuilder<T>
- Type Parameters:
T
- Type of the bean to be populated
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:
- If a mapping strategy is explicitly set, it is always used.
- If
CsvBindByPosition
orCsvCustomBindByPosition
is present,ColumnPositionMappingStrategy
is used. - Otherwise,
HeaderColumnNameMappingStrategy
is used. This includes the case whenCsvBindByName
orCsvCustomBindByName
are being used. The annotations will automatically be recognized.
- Since:
- 3.9
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final CSVReader
Allow the user to pass in a prebuilt/customCSVReader
.private Locale
private Character
private CsvExceptionHandler
private CsvToBeanFilter
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
A CSVReader will be built out of thisReader
.private Character
private Integer
private Boolean
private Boolean
private final List
<BeanVerifier<T>> private Boolean
-
Constructor Summary
ConstructorsConstructorDescriptionCsvToBeanBuilder
(CSVReader csvReader) Constructor with the one parameter that is most definitely mandatory, and always will be.CsvToBeanBuilder
(Reader reader) Constructor with the one parameter that is most definitely mandatory, and always will be. -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds theCsvToBean
out of the provided information.private CSVParser
Builds aCSVParser
from the information provided to this builder.private CSVReader
buildReader
(CSVParser parser) Builds aCSVReader
from the information provided to this builder.withErrorLocale
(Locale errorLocale) Sets the locale for all error messages.withEscapeChar
(char escapeChar) withExceptionHandler
(CsvExceptionHandler exceptionHandler) Sets the handler for recoverable exceptions raised during processing of records.withFieldAsNull
(CSVReaderNullFieldIndicator indicator) withFilter
(CsvToBeanFilter filter) withIgnoreEmptyLine
(boolean ignore) withIgnoreField
(Class<?> type, Field field) Adds aField
to the list of fields opencsv should ignore completely.withIgnoreLeadingWhiteSpace
(boolean ignoreLeadingWhiteSpace) withIgnoreQuotations
(boolean ignoreQuotations) withKeepCarriageReturn
(boolean keepCR) withMappingStrategy
(MappingStrategy<? extends T> mappingStrategy) withMultilineLimit
(int multilineLimit) Sets the maximum number of lines allowed in a multiline record.withOrderedResults
(boolean orderedResults) Sets whether the resulting beans must be ordered as in the input.withProfile
(String profile) Selects a profile for deciding which configurations to use for the bean fields.withQuoteChar
(char quoteChar) withSeparator
(char separator) withSkipLines
(int skipLines) withStrictQuotes
(boolean strictQuotes) withThrowExceptions
(boolean throwExceptions) Sets how the CsvToBean will act when an exception occurs.Sets the type of the bean to be populated.withVerifier
(BeanVerifier<T> verifier) Adds aBeanVerifier
to the list of verifiers to run on all beans created.withVerifyReader
(boolean verifyReader)
-
Field Details
-
mappingStrategy
- See Also:
-
reader
A CSVReader will be built out of thisReader
.- See Also:
-
csvReader
Allow the user to pass in a prebuilt/customCSVReader
. -
filter
- See Also:
-
exceptionHandler
- See Also:
-
nullFieldIndicator
- See Also:
-
keepCR
private boolean keepCR- See Also:
-
skipLines
- See Also:
-
verifyReader
- See Also:
-
separator
- See Also:
-
quoteChar
- See Also:
-
escapeChar
- See Also:
-
strictQuotes
- See Also:
-
ignoreLeadingWhiteSpace
- See Also:
-
ignoreQuotations
- See Also:
-
throwsExceptions
- See Also:
-
type
- See Also:
-
multilineLimit
- See Also:
-
orderedResults
private boolean orderedResults- See Also:
-
ignoreEmptyLines
private boolean ignoreEmptyLines- See Also:
-
errorLocale
- See Also:
-
verifiers
- See Also:
-
ignoredFields
- See Also:
-
profile
- See Also:
-
-
Constructor Details
-
CsvToBeanBuilder
Constructor with the one parameter that is most definitely mandatory, and always will be.- Parameters:
reader
- The reader that is the source of data for the CSV import
-
CsvToBeanBuilder
Constructor with the one parameter that is most definitely mandatory, and always will be.- Parameters:
csvReader
- The CSVReader that is the source of data for the CSV import
-
-
Method Details
-
build
Builds theCsvToBean
out of the provided information.- Returns:
- A valid
CsvToBean
- Throws:
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.
-
buildParser
Builds aCSVParser
from the information provided to this builder. This is an intermediate step in building theCsvToBean
.- Returns:
- An appropriate
CSVParser
-
buildReader
-
withMappingStrategy
- Parameters:
mappingStrategy
- Please see the "See Also" section- Returns:
this
- See Also:
-
withFilter
- Parameters:
filter
- Please see the "See Also" section- Returns:
this
- See Also:
-
withThrowExceptions
Sets how the CsvToBean will act when an exception occurs. If both withThrowsExcpetion andwithExceptionHandler(CsvExceptionHandler)
are used then the withExceptionHandler takes precedence and is used.- Parameters:
throwExceptions
- Please see the "See Also" section- Returns:
this
- See Also:
-
withExceptionHandler
Sets the handler for recoverable exceptions raised during processing of records. If bothwithThrowExceptions(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 isExceptionHandlerThrow
.Please note that if both this method and
withThrowExceptions(boolean)
are called, the last call wins.- Parameters:
exceptionHandler
- The exception handler to be used. Ifnull
, this method does nothing.- Returns:
this
- Since:
- 5.2
-
withFieldAsNull
- Parameters:
indicator
- Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)- Returns:
this
-
withKeepCarriageReturn
- Parameters:
keepCR
- True to keep carriage returns in data read, false otherwise- Returns:
this
-
withVerifyReader
- Parameters:
verifyReader
- Please see the "See Also" section- Returns:
this
- See Also:
-
withSkipLines
- Parameters:
skipLines
- Please see the "See Also" section- Returns:
this
- See Also:
-
withSeparator
- Parameters:
separator
- Please see the "See Also" section- Returns:
this
- See Also:
-
withQuoteChar
- Parameters:
quoteChar
- Please see the "See Also" section- Returns:
this
- See Also:
-
withEscapeChar
- Parameters:
escapeChar
- Please see the "See Also" section- Returns:
this
- See Also:
-
withStrictQuotes
- Parameters:
strictQuotes
- Please see the "See Also" section- Returns:
this
- See Also:
-
withIgnoreLeadingWhiteSpace
- Parameters:
ignoreLeadingWhiteSpace
- Please see the "See Also" section- Returns:
this
- See Also:
-
withIgnoreQuotations
- Parameters:
ignoreQuotations
- Please see the "See Also" section- Returns:
this
- See Also:
-
withType
Sets the type of the bean to be populated. Ignored ifwithMappingStrategy(com.opencsv.bean.MappingStrategy)
is called.- Parameters:
type
- Class of the destination bean- Returns:
this
- See Also:
-
withMultilineLimit
Sets the maximum number of lines allowed in a multiline record. More than this number in one record results in an IOException.- Parameters:
multilineLimit
- No more than this number of lines is allowed in a single input record. The default isCSVReader.DEFAULT_MULTILINE_LIMIT
.- Returns:
this
-
withOrderedResults
Sets whether the resulting beans must be ordered as in the input.- Parameters:
orderedResults
- Whether to order the results or not- Returns:
this
- Since:
- 4.0
- See Also:
-
withErrorLocale
Sets the locale for all error messages.- Parameters:
errorLocale
- Locale for error messages- Returns:
this
- Since:
- 4.0
- See Also:
-
withVerifier
Adds aBeanVerifier
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
- A new verifier that is to process all beans after creation.null
is permissible but has no effect.- Returns:
this
- Since:
- 4.4
-
withIgnoreField
public CsvToBeanBuilder<T> withIgnoreField(Class<?> type, Field field) throws IllegalArgumentException Adds aField
to the list of fields opencsv should ignore completely.May be called as many times as necessary.
- Parameters:
type
- The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.field
- The field opencsv is to ignore- Returns:
this
- Throws:
IllegalArgumentException
- If one of the parameters isnull
orfield
cannot be found intype
.- Since:
- 5.0
- See Also:
-
withIgnoreEmptyLine
- Parameters:
ignore
- Please see the "See Also" section- Returns:
this
- See Also:
-
withProfile
Selects a profile for deciding which configurations to use for the bean fields.- Parameters:
profile
- The name of the profile to be used- Returns:
this
- Since:
- 5.4
-