Class CSVReader

java.lang.Object
fr.becpg.common.csv.CSVReader
All Implemented Interfaces:
AutoCloseable

public class CSVReader extends Object implements AutoCloseable
A very simple CSV reader released under a commercial-friendly license.
Version:
$Id: $Id
Author:
Glen Smith
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final BufferedReader
     
    static final char
    The default quote character to use if none is supplied to the constructor.
    static final char
    The default separator to use if none is supplied to the constructor.
    static final int
    The default line to start reading.
    private boolean
     
    private boolean
     
    private final char
     
    private final char
     
    private final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    CSVReader(Reader reader)
    Constructs CSVReader using a comma for the separator.
    CSVReader(Reader reader, char separator)
    Constructs CSVReader with supplied separator.
    CSVReader(Reader reader, char separator, char quotechar)
    Constructs CSVReader with supplied separator and quote char.
    CSVReader(Reader reader, char separator, char quotechar, int line)
    Constructs CSVReader with supplied separator and quote char.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes the underlying reader.
    private String
    Reads the next line from the file.
    private String[]
    parseLine(String nextLine)
    Parses an incoming String and returns an array of elements.
    Reads the entire file into a List with each element being a String[] of tokens.
    Reads the next line from the buffer and converts to a string array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • br

      private final BufferedReader br
    • hasNext

      private boolean hasNext
    • separator

      private final char separator
    • quotechar

      private final char quotechar
    • skipLines

      private final int skipLines
    • linesSkiped

      private boolean linesSkiped
    • DEFAULT_SEPARATOR

      public static final char DEFAULT_SEPARATOR
      The default separator to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_QUOTE_CHARACTER

      public static final char DEFAULT_QUOTE_CHARACTER
      The default quote character to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_SKIP_LINES

      public static final int DEFAULT_SKIP_LINES
      The default line to start reading.
      See Also:
  • Constructor Details

    • CSVReader

      public CSVReader(Reader reader)
      Constructs CSVReader using a comma for the separator.
      Parameters:
      reader - the reader to an underlying CSV source.
    • CSVReader

      public CSVReader(Reader reader, char separator)
      Constructs CSVReader with supplied separator.
      Parameters:
      reader - the reader to an underlying CSV source.
      separator - the delimiter to use for separating entries.
    • CSVReader

      public CSVReader(Reader reader, char separator, char quotechar)
      Constructs CSVReader with supplied separator and quote char.
      Parameters:
      reader - the reader to an underlying CSV source.
      separator - the delimiter to use for separating entries
      quotechar - the character to use for quoted elements
    • CSVReader

      public CSVReader(Reader reader, char separator, char quotechar, int line)
      Constructs CSVReader with supplied separator and quote char.
      Parameters:
      reader - the reader to an underlying CSV source.
      separator - the delimiter to use for separating entries
      quotechar - the character to use for quoted elements
      line - the line number to skip for start reading
  • Method Details

    • readAll

      public List<String[]> readAll() throws IOException
      Reads the entire file into a List with each element being a String[] of tokens.
      Returns:
      a List of String[], with each String[] representing a line of the file.
      Throws:
      IOException - if any.
    • readNext

      public String[] readNext() throws IOException
      Reads the next line from the buffer and converts to a string array.
      Returns:
      a string array with each comma-separated element as a separate entry.
      Throws:
      IOException - if any.
    • getNextLine

      private String getNextLine() throws IOException
      Reads the next line from the file.
      Returns:
      the next line from the file without trailing newline
      Throws:
      IOException - if bad things happen during the read
    • parseLine

      private String[] parseLine(String nextLine) throws IOException
      Parses an incoming String and returns an array of elements.
      Parameters:
      nextLine - the string to parse
      Returns:
      the comma-tokenized list of elements, or null if nextLine is null
      Throws:
      IOException - if bad things happen during the read
    • close

      public void close() throws IOException
      Closes the underlying reader.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException