Class Csv


  • public class Csv
    extends java.lang.Object
    A facility to read from and write to CSV (comma separated values) files. This is a simplified version of the CSV tool of the H2 database: http://code.google.com/p/h2database/source/browse/trunk/h2/src/main/org/h2/tools/Csv.java (also written by Thomas Mueller)
    • Constructor Summary

      Constructors 
      Constructor Description
      Csv()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close all streams.
      char getEscapeCharacter()
      Get the current escape character.
      char getFieldDelimiter()
      Get the current field delimiter.
      char getFieldSeparatorRead()
      Get the current field separator for reading.
      java.lang.String getFieldSeparatorWrite()
      Get the current field separator for writing.
      java.lang.String getNullString()
      Get the current null string.
      java.lang.String getRowSeparatorWrite()
      Get the current row separator for writing.
      java.util.Iterator<java.lang.String[]> read​(java.io.InputStream in, java.lang.String charset)
      Reads from the CSV file and returns an iterator.
      java.util.Iterator<java.lang.String[]> read​(java.io.Reader reader)
      Reads from the CSV file and returns an iterator.
      void setEscapeCharacter​(char escapeCharacter)
      Set the escape character (used to escape the field delimiter).
      void setFieldDelimiter​(char fieldDelimiter)
      Set the field delimiter.
      void setFieldSeparatorRead​(char fieldSeparatorRead)
      Override the field separator for reading.
      void setFieldSeparatorWrite​(java.lang.String fieldSeparatorWrite)
      Override the field separator for writing.
      void setLineSeparator​(java.lang.String lineSeparator)
      Set the line separator.
      void setNullString​(java.lang.String nullString)
      Set the value that represents NULL.
      void setRowSeparatorWrite​(java.lang.String rowSeparatorWrite)
      Override the end-of-row marker for writing.
      void writeInit​(java.io.OutputStream out, java.lang.String charset)
      Initialize writing.
      void writeInit​(java.io.Writer writer)
      Initialize writing.
      void writeRow​(java.lang.String... values)
      Write a row.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Csv

        public Csv()
    • Method Detail

      • read

        public java.util.Iterator<java.lang.String[]> read​(java.io.InputStream in,
                                                           java.lang.String charset)
                                                    throws java.io.IOException
        Reads from the CSV file and returns an iterator. The rows in the result set are created on demand, that means the file is kept open until all rows are read or the CSV tool is closed.
        Depending on the contents of the file, the first line of the result may or may not contain the column names.
        Parameters:
        in - the input stream
        charset - the charset or null to use the system default charset (see system property file.encoding)
        Returns:
        the iterator
        Throws:
        java.io.IOException
      • read

        public java.util.Iterator<java.lang.String[]> read​(java.io.Reader reader)
                                                    throws java.io.IOException
        Reads from the CSV file and returns an iterator. The rows in the result set are created on demand, that means the file is kept open until all rows are read or the CSV tool is closed.
        Depending on the contents of the file, the first line of the result may or may not contain the column names.
        Parameters:
        reader - the reader
        Returns:
        the iterator
        Throws:
        java.io.IOException
      • writeInit

        public void writeInit​(java.io.OutputStream out,
                              java.lang.String charset)
                       throws java.io.IOException
        Initialize writing.
        Parameters:
        out - the output stream
        charset - the character set or null
        Throws:
        java.io.IOException
      • writeInit

        public void writeInit​(java.io.Writer writer)
                       throws java.io.IOException
        Initialize writing.
        Parameters:
        writer - the writer
        Throws:
        java.io.IOException
      • writeRow

        public void writeRow​(java.lang.String... values)
                      throws java.io.IOException
        Write a row.
        Parameters:
        values - the values
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Close all streams. Exceptions are ignored.
        Throws:
        java.io.IOException
      • setFieldSeparatorWrite

        public void setFieldSeparatorWrite​(java.lang.String fieldSeparatorWrite)
        Override the field separator for writing. The default is ",".
        Parameters:
        fieldSeparatorWrite - the field separator
      • getFieldSeparatorWrite

        public java.lang.String getFieldSeparatorWrite()
        Get the current field separator for writing.
        Returns:
        the field separator
      • setFieldSeparatorRead

        public void setFieldSeparatorRead​(char fieldSeparatorRead)
        Override the field separator for reading. The default is ','.
        Parameters:
        fieldSeparatorRead - the field separator
      • getFieldSeparatorRead

        public char getFieldSeparatorRead()
        Get the current field separator for reading.
        Returns:
        the field separator
      • getRowSeparatorWrite

        public java.lang.String getRowSeparatorWrite()
        Get the current row separator for writing.
        Returns:
        the row separator
      • setRowSeparatorWrite

        public void setRowSeparatorWrite​(java.lang.String rowSeparatorWrite)
        Override the end-of-row marker for writing. The default is null. After writing the end-of-row marker, a line feed is written (\n or \r\n depending on the system settings).
        Parameters:
        rowSeparatorWrite - the row separator
      • setFieldDelimiter

        public void setFieldDelimiter​(char fieldDelimiter)
        Set the field delimiter. The default is " (a double quote). The value 0 means no field delimiter is used.
        Parameters:
        fieldDelimiter - the field delimiter
      • getFieldDelimiter

        public char getFieldDelimiter()
        Get the current field delimiter.
        Returns:
        the field delimiter
      • setEscapeCharacter

        public void setEscapeCharacter​(char escapeCharacter)
        Set the escape character (used to escape the field delimiter). The default is " (a double quote). The value 0 means no escape character is used.
        Parameters:
        escapeCharacter - the escape character
      • getEscapeCharacter

        public char getEscapeCharacter()
        Get the current escape character.
        Returns:
        the escape character
      • setLineSeparator

        public void setLineSeparator​(java.lang.String lineSeparator)
        Set the line separator.
        Parameters:
        lineSeparator - the line separator
      • setNullString

        public void setNullString​(java.lang.String nullString)
        Set the value that represents NULL. The default is an empty string.
        Parameters:
        nullString - the null
      • getNullString

        public java.lang.String getNullString()
        Get the current null string.
        Returns:
        the null string.