Class CsvReader

java.lang.Object
org.apache.juneau.marshall.csv.CsvReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class CsvReader extends Object implements Closeable
Specialized reader for parsing CSV input.

Parses CSV data according to RFC 4180 with support for:

  • Quoted fields (configurable quote character)
  • Embedded delimiter characters within quoted fields
  • Embedded newlines within quoted fields
  • Embedded quote characters escaped by doubling (RFC 4180 style)
  • Both CRLF and LF line endings
  • Optional whitespace trimming
Notes:
  • This class is not intended for external use.
  • Constructor Details

    • CsvReader

      public CsvReader(ParserReader r, char delimiter, char quoteChar, boolean trimStrings)
      Constructor.
      Parameters:
      r - The parser reader to wrap.
      Must not be null.
      delimiter - The field delimiter character (typically ',').
      quoteChar - The quote character (typically '"').
      trimStrings - If true, field values are trimmed of surrounding whitespace.
  • Method Details

    • from

      public static CsvReader from(ParserPipe pipe, char delimiter, char quoteChar, boolean trimStrings) throws IOException
      Creates a CsvReader from a ParserPipe.
      Parameters:
      pipe - The parser pipe.
      Must not be null.
      delimiter - The delimiter character.
      quoteChar - The quote character.
      trimStrings - Whether to trim strings.
      Returns:
      A new CsvReader, or null if the pipe has no input.
      Throws:
      IOException - Thrown by underlying stream.
    • readRow

      Reads a single row of CSV data, returning null at end of input.

      Each call advances past one record (terminated by LF, CRLF, or end-of-file). Quoted fields may span multiple lines.

      Returns:
      A list of field values for the row, or null if end of input has been reached.
      Throws:
      IOException - Thrown by underlying stream.
      ParseException - If the CSV is malformed (e.g. an unclosed quoted field).
    • isEof

      public boolean isEof()
      Returns whether the end of input has been reached.
      Returns:
      true if end of input has been reached.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException