Package com.opencsv

Class CSVReaderBuilder

  • Direct Known Subclasses:
    CSVReaderHeaderAwareBuilder

    public class CSVReaderBuilder
    extends java.lang.Object
    Builder for creating a CSVReader.

    This should be the preferred method of creating a Reader as there are so many possible values to be set it is impossible to have constructors for all of them and keep backwards compatibility with previous constructors.
    final CSVParser parser =
    new CSVParserBuilder()
    .withSeparator('\t')
    .withIgnoreQuotations(true)
    .build();
    final CSVReader reader =
    new CSVReaderBuilder(new StringReader(csv))
    .withSkipLines(1)
    .withCSVParser(parser)
    .build();

    See Also:
    CSVReader
    • Constructor Detail

      • CSVReaderBuilder

        public CSVReaderBuilder​(java.io.Reader reader)
        Sets the reader to an underlying CSV source.
        Parameters:
        reader - The reader to an underlying CSV source.
    • Method Detail

      • getReader

        protected java.io.Reader getReader()
        Used by unit tests.
        Returns:
        The reader.
      • getSkipLines

        protected int getSkipLines()
        Used by unit tests.
        Returns:
        The set number of lines to skip
      • getCsvParser

        protected ICSVParser getCsvParser()
        Used by unit tests.
        Returns:
        The CSVParser used by the builder.
      • getMultilineLimit

        protected int getMultilineLimit()
        Used by unit tests.
        Returns:
        The upper limit on lines in multiline records.
      • withSkipLines

        public CSVReaderBuilder withSkipLines​(int skipLines)
        Sets the number of lines to skip before reading.
        Parameters:
        skipLines - The number of lines to skip before reading.
        Returns:
        this
      • withCSVParser

        public CSVReaderBuilder withCSVParser​(ICSVParser icsvParser)
        Sets the parser to use to parse the input.
        Parameters:
        icsvParser - The parser to use to parse the input.
        Returns:
        this
      • build

        public CSVReader build()
        Creates the CSVReader.
        Returns:
        The CSVReader based on the set criteria.
      • withKeepCarriageReturn

        public CSVReaderBuilder withKeepCarriageReturn​(boolean keepCR)
        Sets if the reader will keep or discard carriage returns.
        Parameters:
        keepCR - True to keep carriage returns, false to discard.
        Returns:
        this
      • keepCarriageReturn

        protected boolean keepCarriageReturn()
        Returns if the reader built will keep or discard carriage returns.
        Returns:
        true if the reader built will keep carriage returns, false otherwise
      • getOrCreateCsvParser

        protected ICSVParser getOrCreateCsvParser()
        Creates a new ICSVParser if the class does't already hold one.
        Returns:
        The injected ICSVParser or a default parser.
      • withVerifyReader

        public CSVReaderBuilder withVerifyReader​(boolean verifyReader)
        Checks to see if the CSVReader should verify the reader state before reads or not.

        This should be set to false if you are using some form of asynchronous reader (like readers created by the java.nio.* classes).

        The default value is true.

        Parameters:
        verifyReader - True if CSVReader should verify reader before each read, false otherwise.
        Returns:
        this
      • isVerifyReader

        public boolean isVerifyReader()
        Returns:
        The flag indicating whether the reader should be verified before each read.
      • withFieldAsNull

        public CSVReaderBuilder withFieldAsNull​(CSVReaderNullFieldIndicator indicator)
        Checks to see if it should treat a field with two separators, two quotes, or both as a null field.
        Parameters:
        indicator - CSVReaderNullFieldIndicator set to what should be considered a null field.
        Returns:
        this
      • withMultilineLimit

        public CSVReaderBuilder 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 - No more than this number of lines is allowed in a single input record. The default is CSVReader.DEFAULT_MULTILINE_LIMIT.
        Returns:
        this
      • withErrorLocale

        public CSVReaderBuilder withErrorLocale​(java.util.Locale errorLocale)
        Sets the locale for all error messages.
        Parameters:
        errorLocale - Locale for error messages
        Returns:
        this
        Since:
        4.0
      • getErrorLocale

        public java.util.Locale getErrorLocale()
        Returns:
        The locale for error messages
      • getLineValidatorAggregator

        public LineValidatorAggregator getLineValidatorAggregator()
        Returns:
        The LineValidatorAggragator for custom defined LineValidators.
      • getRowValidatorAggregator

        public RowValidatorAggregator getRowValidatorAggregator()
        Returns:
        The RowValidatorAggregator for the custom defined RowValidators.
      • withLineValidator

        public CSVReaderBuilder withLineValidator​(LineValidator lineValidator)
        Adds a LineValidator to the CSVReader. Multiple LineValidators can be added with multiple calls.
        Parameters:
        lineValidator - LineValidator to inject.
        Returns:
        this
        Since:
        5.0
      • withRowValidator

        public CSVReaderBuilder withRowValidator​(RowValidator rowValidator)
        Adds a RowValidator to the CSVReader. Multiple RowValidators can be added with multiple calls.
        Parameters:
        rowValidator - RowValidator to inject
        Returns:
        this
        Since:
        5.0
      • withRowProcessor

        public CSVReaderBuilder withRowProcessor​(RowProcessor rowProcessor)
        Adds a RowProcessor to the CSVReader. Only a single RowProcessor can be added so multiple calls will overwrite the previously set RowProcessor.
        Parameters:
        rowProcessor - RowProcessor to inject
        Returns:
        this
        Since:
        5.0