T
- Type of the bean to be populatedpublic class CsvToBeanBuilder<T> extends Object
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:
CsvBindByPosition
or CsvCustomBindByPosition
is present,
ColumnPositionMappingStrategy
is used.HeaderColumnNameMappingStrategy
is used. This includes
the case when CsvBindByName
or CsvCustomBindByName
are being
used. The annotations will automatically be recognized.Constructor and Description |
---|
CsvToBeanBuilder(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.
|
Modifier and Type | Method and Description |
---|---|
CsvToBean<T> |
build()
Builds the
CsvToBean out of the provided information. |
CsvToBeanBuilder<T> |
withErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
CsvToBeanBuilder<T> |
withEscapeChar(char escapeChar) |
CsvToBeanBuilder<T> |
withExceptionHandler(CsvExceptionHandler exceptionHandler)
Sets the handler for recoverable exceptions raised during processing of
records.
|
CsvToBeanBuilder<T> |
withFieldAsNull(CSVReaderNullFieldIndicator indicator) |
CsvToBeanBuilder<T> |
withFilter(CsvToBeanFilter filter) |
CsvToBeanBuilder<T> |
withIgnoreEmptyLine(boolean ignore) |
CsvToBeanBuilder<T> |
withIgnoreField(Class<?> type,
Field field)
Adds a
Field to the list of fields opencsv should ignore
completely. |
CsvToBeanBuilder<T> |
withIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace) |
CsvToBeanBuilder<T> |
withIgnoreQuotations(boolean ignoreQuotations) |
CsvToBeanBuilder<T> |
withKeepCarriageReturn(boolean keepCR) |
CsvToBeanBuilder<T> |
withMappingStrategy(MappingStrategy<? extends T> mappingStrategy) |
CsvToBeanBuilder<T> |
withMultilineLimit(int multilineLimit)
Sets the maximum number of lines allowed in a multiline record.
|
CsvToBeanBuilder<T> |
withOrderedResults(boolean orderedResults)
Sets whether the resulting beans must be ordered as in the input.
|
CsvToBeanBuilder<T> |
withProfile(String profile)
Selects a profile for deciding which configurations to use for the bean
fields.
|
CsvToBeanBuilder<T> |
withQuoteChar(char quoteChar) |
CsvToBeanBuilder<T> |
withSeparator(char separator) |
CsvToBeanBuilder<T> |
withSkipLines(int skipLines) |
CsvToBeanBuilder<T> |
withStrictQuotes(boolean strictQuotes) |
CsvToBeanBuilder<T> |
withThrowExceptions(boolean throwExceptions)
Sets how the CsvToBean will act when an exception occurs.
|
CsvToBeanBuilder<T> |
withType(Class<? extends T> type)
Sets the type of the bean to be populated.
|
CsvToBeanBuilder<T> |
withVerifier(BeanVerifier<T> verifier)
Adds a
BeanVerifier to the list of verifiers to run on all
beans created. |
CsvToBeanBuilder<T> |
withVerifyReader(boolean verifyReader) |
public CsvToBeanBuilder(Reader reader)
reader
- The reader that is the source of data for the CSV importpublic CsvToBeanBuilder(CSVReader csvReader)
csvReader
- The CSVReader that is the source of data for the CSV importpublic CsvToBean<T> build() throws IllegalStateException
CsvToBean
out of the provided information.CsvToBean
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.public CsvToBeanBuilder<T> withMappingStrategy(MappingStrategy<? extends T> mappingStrategy)
mappingStrategy
- Please see the "See Also" sectionthis
CsvToBean.setMappingStrategy(com.opencsv.bean.MappingStrategy)
public CsvToBeanBuilder<T> withFilter(CsvToBeanFilter filter)
filter
- Please see the "See Also" sectionthis
CsvToBean.setFilter(com.opencsv.bean.CsvToBeanFilter)
public CsvToBeanBuilder<T> withThrowExceptions(boolean throwExceptions)
withExceptionHandler(CsvExceptionHandler)
are used then the withExceptionHandler takes
precedence and is used.throwExceptions
- Please see the "See Also" sectionthis
CsvToBean.setThrowExceptions(boolean)
,
withExceptionHandler(CsvExceptionHandler)
public CsvToBeanBuilder<T> withExceptionHandler(CsvExceptionHandler exceptionHandler)
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.
exceptionHandler
- The exception handler to be used. If null
,
this method does nothing.this
public CsvToBeanBuilder<T> withFieldAsNull(CSVReaderNullFieldIndicator indicator)
indicator
- Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES,
BOTH, NEITHER (default)this
public CsvToBeanBuilder<T> withKeepCarriageReturn(boolean keepCR)
keepCR
- True to keep carriage returns in data read, false otherwisethis
public CsvToBeanBuilder<T> withVerifyReader(boolean verifyReader)
verifyReader
- Please see the "See Also" sectionthis
CSVReaderBuilder.withVerifyReader(boolean)
public CsvToBeanBuilder<T> withSkipLines(int skipLines)
skipLines
- Please see the "See Also" sectionthis
CSVReaderBuilder.withSkipLines(int)
public CsvToBeanBuilder<T> withSeparator(char separator)
separator
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withQuoteChar(char quoteChar)
quoteChar
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withEscapeChar(char escapeChar)
escapeChar
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withStrictQuotes(boolean strictQuotes)
strictQuotes
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace)
ignoreLeadingWhiteSpace
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withIgnoreQuotations(boolean ignoreQuotations)
ignoreQuotations
- Please see the "See Also" sectionthis
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
public CsvToBeanBuilder<T> withType(Class<? extends T> type)
withMappingStrategy(com.opencsv.bean.MappingStrategy)
is called.type
- Class of the destination beanthis
AbstractMappingStrategy.setType(java.lang.Class)
,
AbstractMappingStrategy.setType(java.lang.Class)
public CsvToBeanBuilder<T> withMultilineLimit(int multilineLimit)
multilineLimit
- No more than this number of lines is allowed in a
single input record. The default is CSVReader.DEFAULT_MULTILINE_LIMIT
.this
public CsvToBeanBuilder<T> withOrderedResults(boolean orderedResults)
orderedResults
- Whether to order the results or notthis
CsvToBean.setOrderedResults(boolean)
public CsvToBeanBuilder<T> withErrorLocale(Locale errorLocale)
errorLocale
- Locale for error messagesthis
CsvToBean.setErrorLocale(java.util.Locale)
public CsvToBeanBuilder<T> withVerifier(BeanVerifier<T> verifier)
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.verifier
- A new verifier that is to process all beans after
creation. null
is permissible but has no effect.this
public CsvToBeanBuilder<T> withIgnoreField(Class<?> type, Field field) throws IllegalArgumentException
Field
to the list of fields opencsv should ignore
completely.
May be called as many times as necessary.
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 ignorethis
IllegalArgumentException
- If one of the parameters is
null
or field
cannot be found in type
.MappingStrategy.ignoreFields(MultiValuedMap)
public CsvToBeanBuilder<T> withIgnoreEmptyLine(boolean ignore)
ignore
- Please see the "See Also" sectionthis
CsvToBean.ignoreEmptyLines
public CsvToBeanBuilder<T> withProfile(String profile)
profile
- The name of the profile to be usedthis
Copyright © 2005–2023. All rights reserved.