Class CsvToBean<T>

java.lang.Object
com.opencsv.bean.CsvToBean<T>
Type Parameters:
T - Class to convert the objects to.
All Implemented Interfaces:
Iterable<T>

public class CsvToBean<T> extends Object implements Iterable<T>
Converts CSV data to objects. Mixing the parse() method with the Iterator is not supported and will lead to unpredictable results. Additionally, reusing an instance of this class after all beans have been read is not supported and will certainly break something.
  • Field Details

    • capturedExceptions

      private final List<CsvException> capturedExceptions
      A list of all exceptions during parsing and mapping of the input.
    • mappingStrategy

      private MappingStrategy<? extends T> mappingStrategy
      The mapping strategy to be used by this CsvToBean.
    • csvReader

      private CSVReader csvReader
      The reader this class will use to access the data to be read.
    • filter

      private CsvToBeanFilter filter
      The filter this class will use on the beans it reads.
    • exceptionHandler

      private CsvExceptionHandler exceptionHandler
      Determines how exceptions thrown during processing will be handled.
    • orderedResults

      private boolean orderedResults
      Determines whether resulting data sets have to be in the same order as the input.
    • executor

      private LineExecutor<T> executor
      The ExecutorService for parallel processing of beans.
    • errorLocale

      private Locale errorLocale
      The errorLocale for error messages.
    • verifiers

      private List<BeanVerifier<T>> verifiers
      All verifiers that should be run on beans after creation but before returning them to the caller.
    • ignoreEmptyLines

      private boolean ignoreEmptyLines
      When an empty line is encountered (not part of the data) then it is ignored. By default this is false, which means an exception is thrown if there are required fields or the number of fields do not match the number of headers.
  • Constructor Details

    • CsvToBean

      public CsvToBean()
      Default constructor.
  • Method Details

    • parse

      public List<T> parse() throws IllegalStateException
      Parses the input based on parameters already set through other methods.
      Returns:
      A list of populated beans based on the input
      Throws:
      IllegalStateException - If either MappingStrategy or CSVReader is not specified
      See Also:
    • stream

      public Stream<T> stream() throws IllegalStateException
      Parses the input based on parameters already set through other methods. This method saves a marginal amount of time and storage compared to parse() because it avoids the intermediate storage of the results in a List. If you plan on further processing the results as a Stream, use this method.
      Returns:
      A stream of populated beans based on the input
      Throws:
      IllegalStateException - If either MappingStrategy or CSVReader is not specified
      See Also:
    • getCapturedExceptions

      public List<CsvException> getCapturedExceptions()
      Returns the list of all exceptions that would have been thrown during the import, but were queued by the exception handler.

      The results returned by this method are not consistent until parsing is concluded.

      Returns:
      The list of exceptions captured while processing the input file
      See Also:
    • setMappingStrategy

      public void setMappingStrategy(MappingStrategy<? extends T> mappingStrategy)
      Sets the mapping strategy to be used by this bean.
      Parameters:
      mappingStrategy - Mapping strategy to convert CSV input to a bean
    • setCsvReader

      public void setCsvReader(CSVReader csvReader)
      Sets the reader to be used to read in the information from the CSV input.
      Parameters:
      csvReader - Reader for input
    • setFilter

      public void setFilter(CsvToBeanFilter filter)
      Sets a filter to selectively remove some lines of input before they become beans.
      Parameters:
      filter - A class that filters the input lines
    • setThrowExceptions

      public void setThrowExceptions(boolean throwExceptions)
      Determines whether errors during import should be thrown or kept in a list for later retrieval via getCapturedExceptions().

      This is a convenience function and is maintained for backwards compatibility. Passing in true is equivalent to setExceptionHandler(new ExceptionHandlerThrow()) and false is equivalent to setExceptionHandler(new ExceptionHandlerQueue())

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

      Parameters:
      throwExceptions - Whether or not to throw exceptions during processing
      See Also:
    • setExceptionHandler

      public void setExceptionHandler(CsvExceptionHandler handler)
      Sets the handler for recoverable exceptions raised during processing of records.

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

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

      Parameters:
      handler - The exception handler to be used. If null, this method does nothing.
      Since:
      5.2
    • getExceptionHandler

      CsvExceptionHandler getExceptionHandler()
      Package scope method currently used by the CsvToBeanBuilderTest
      Returns:
      CsvExceptionHandler used by the CsvToBean object.
    • setOrderedResults

      public void setOrderedResults(boolean orderedResults)
      Sets whether or not results must be returned in the same order in which they appear in the 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 setting orderedResults to false. 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 beans returned are in the same order they appeared in the input
      Since:
      4.0
    • setErrorLocale

      public void setErrorLocale(Locale errorLocale)
      Sets the locale for error messages.
      Parameters:
      errorLocale - Locale for error messages. If null, the default locale is used.
      Since:
      4.0
    • setVerifiers

      public void setVerifiers(List<BeanVerifier<T>> verifiers)
      Sets the list of verifiers to be run on all beans after creation.
      Parameters:
      verifiers - A list of verifiers. May be null, in which case, no verifiers are run.
      Since:
      4.4
    • prepareToReadInput

      private void prepareToReadInput() throws IllegalStateException
      Throws:
      IllegalStateException
    • iterator

      public Iterator<T> iterator()
      The iterator returned by this method takes one line of input at a time and returns one bean at a time.

      The advantage to this method is saving memory. The cost is the loss of parallel processing, reducing throughput.

      The iterator respects all aspects of CsvToBean, including filters and capturing exceptions.

      Specified by:
      iterator in interface Iterable<T>
      Returns:
      An iterator over the beans created from the input
      See Also:
    • setIgnoreEmptyLines

      public void setIgnoreEmptyLines(boolean ignoreEmptyLines)
      Ignores any blank lines in the data that are not part of a field.
      Parameters:
      ignoreEmptyLines - true to ignore empty lines, false otherwise