Class StatefulBeanToCsv<T>
java.lang.Object
com.opencsv.bean.StatefulBeanToCsv<T>
- Type Parameters:
T- Type of the bean to be written
This class writes beans out in CSV format to a
Writer,
keeping state information and making an intelligent guess at the mapping
strategy to be applied.
This class implements multi-threading on writing more than one bean, so there should be no need to use it across threads in an application. As such, it is not thread-safe.
- Since:
- 3.9
- Author:
- Andrew Rucker Jones
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionStatefulBeanToCsv(MappingStrategy<T> mappingStrategy, CsvExceptionHandler exceptionHandler, boolean applyQuotesToAll, ICSVWriter csvWriter, org.apache.commons.collections4.MultiValuedMap<Class<?>, Field> ignoredFields, String profile) Constructor used to allow building of aStatefulBeanToCsvwith a user-suppliedICSVWriterclass. -
Method Summary
Modifier and TypeMethodDescriptionAny exceptions captured during writing of beans to a CSV destination can be retrieved through this method.booleanDeprecated.There is simply no need for this method.voidsetErrorLocale(Locale errorLocale) Sets the locale for all error messages.voidsetOrderedResults(boolean orderedResults) Sets whether or not results must be written in the same order in which they appear in the list of beans provided as input.voidWrites an iterator of beans out to theWriterprovided to the constructor.voidWrites a list of beans out to theWriterprovided to the constructor.voidWrites a stream of beans out to theWriterprovided to the constructor.voidWrites a bean out to theWriterprovided to the constructor.
-
Constructor Details
-
StatefulBeanToCsv
public StatefulBeanToCsv(MappingStrategy<T> mappingStrategy, CsvExceptionHandler exceptionHandler, boolean applyQuotesToAll, ICSVWriter csvWriter, org.apache.commons.collections4.MultiValuedMap<Class<?>, Field> ignoredFields, String profile) Constructor used to allow building of aStatefulBeanToCsvwith a user-suppliedICSVWriterclass.- Parameters:
mappingStrategy- The mapping strategy to use when writing a CSV fileexceptionHandler- Determines the exception handling behaviorapplyQuotesToAll- Whether all output fields should be quotedcsvWriter- An user-suppliedICSVWriterfor writing beans to a CSV outputignoredFields- The fields to ignore during processing. May benull.profile- The profile to use when configuring how to interpret bean fields
-
-
Method Details
-
write
Writes a bean out to theWriterprovided to the constructor.- Parameters:
bean- A bean to be written to a CSV destination- Throws:
CsvDataTypeMismatchException- If a field of the bean is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException- If a field is marked as required, but the source is null
-
write
public void write(List<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException Writes a list of beans out to theWriterprovided to the constructor.- Parameters:
beans- A list of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException- If a field is marked as required, but the source is null
-
write
public void write(Iterator<T> iBeans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException Writes an iterator of beans out to theWriterprovided to the constructor.- Parameters:
iBeans- An iterator of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException- If a field is marked as required, but the source is null
-
write
public void write(Stream<T> beans) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException Writes a stream of beans out to theWriterprovided to the constructor.- Parameters:
beans- A stream of beans to be written to a CSV destination- Throws:
CsvDataTypeMismatchException- If a field of the beans is annotated improperly or an unsupported data type is supposed to be writtenCsvRequiredFieldEmptyException- If a field is marked as required, but the source is null
-
setOrderedResults
public void setOrderedResults(boolean orderedResults) Sets whether or not results must be written in the same order in which they appear in the list of beans provided as input. The default is that order is preserved. If your data do not need to be ordered, you can get a slight performance boost by settingorderedResultstofalse. The lack of ordering then also applies to any captured exceptions, if you have chosen not to have exceptions thrown.- Parameters:
orderedResults- Whether or not the lines written are in the same order they appeared in the input- Since:
- 4.0
-
isThrowExceptions
Deprecated.There is simply no need for this method.- Returns:
- Whether or not exceptions are thrown. If they are not thrown,
they are captured and returned later via
getCapturedExceptions().
-
getCapturedExceptions
Any exceptions captured during writing of beans to a CSV destination can be retrieved through this method.Reads from the list are destructive! Calling this method will clear the list of captured exceptions. However, calling
write(java.util.List)orwrite(java.lang.Object)multiple times with no intervening call to this method will not clear the list of captured exceptions, but rather add to it if further exceptions are thrown.- Returns:
- A list of exceptions that would have been thrown during any and all read operations since the last call to this method
-
setErrorLocale
Sets the locale for all error messages.- Parameters:
errorLocale- Locale for error messages. If null, the default locale is used.- Since:
- 4.0
-