Class CSVDataSource

java.lang.Object
org.pepsoft.util.CSVDataSource

public class CSVDataSource extends Object
Utility class for reading and writing RFC 4180 CSV files. This class does not support line breaks in fields, and it only supports files with headers.
  • Constructor Details

    • CSVDataSource

      public CSVDataSource()
  • Method Details

    • openForReading

      public void openForReading(Reader in) throws IOException
      Open a CSV formatted character stream for reading. A CSVDataSource that has been used for reading once cannot be reused.
      Parameters:
      in - The character stream to read.
      Throws:
      IOException - If an I/O error occurs reading the headers or the first row of data.
    • openForWriting

      public void openForWriting(Writer out, String... columnNames) throws IOException
      Open a CSV formatted character stream for writing. A CSVDataSource that has been used for writing once cannot be reused.
      Parameters:
      out - The character stream to write to.
      columnNames - The names of all the columns that will be written.
      Throws:
      IOException - If an I/O error occurs writing the headers.
    • isEndOfFile

      public boolean isEndOfFile()
      Indicates whether the end of the file has been reached. Only applicable when reading.
      Returns:
      true if the end of the file has been reached.
    • next

      public void next() throws IOException
      Advance to the next row, if any. When writing this will write a row of values to the stream. When reading, isEndOfFile() should be invoked afterwards, and before trying to get data, to determine whether the end of the file had been reached.
      Throws:
      IOException - If an I/O error occurs reading from or writing to the stream.
    • getString

      public String getString(String columnName)
      Get a string-typed value by column name.
      Parameters:
      columnName - The name of the column.
      Returns:
      The value of the specified column in the current row.
    • getString

      public String getString(String columnName, String defaultValue)
      Get a string-typed value by column name.
      Parameters:
      columnName - The name of the column.
      defaultValue - The value to return if the specified column is not present, or the value is not set.
      Returns:
      The value of the specified column in the current row.
    • getString

      public String getString(int columnIndex)
      Get a string-typed value by column index.
      Parameters:
      columnIndex - The index of the column.
      Returns:
      The value of the specified column in the current row.
    • getString

      public String getString(int columnIndex, String defaultValue)
      Get a string-typed value by column index.
      Parameters:
      columnIndex - The index of the column.
      Returns:
      The value of the specified column in the current row, or defaultValue if the column does not exist or the value is not set.
    • setString

      public void setString(String columnName, String value)
      Set a string-typed value by column name. null values are supported but are converted into empty strings.
      Parameters:
      columnName - The name of the column.
      value - The value to store in the column.
    • setString

      public void setString(int columnIndex, String value)
      Set a string-typed value by column index. null values are supported but are converted into empty strings.
      Parameters:
      columnIndex - The index of the column.
      value - The value to store in the column.
    • getInt

      public int getInt(String columnName)
    • getInt

      public int getInt(String columnName, int defaultValue)
    • setInt

      public void setInt(String columnName, int value)
    • getBoolean

      public boolean getBoolean(String columnName)
    • getBoolean

      public boolean getBoolean(String columnName, boolean defaultValue)
    • setBoolean

      public void setBoolean(String columnName, boolean value)