Class Csv


  • public class Csv
    extends 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 Detail

      • Csv

        public Csv()
    • Method Detail

      • read

        public Iterator<String[]> read​(InputStream in,
                                       String charset)
                                throws 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:
        IOException
      • read

        public Iterator<String[]> read​(Reader reader)
                                throws 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:
        IOException
      • writeInit

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

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

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

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

        public 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 String getRowSeparatorWrite()
        Get the current row separator for writing.
        Returns:
        the row separator
      • setRowSeparatorWrite

        public void setRowSeparatorWrite​(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​(String lineSeparator)
        Set the line separator.
        Parameters:
        lineSeparator - the line separator
      • setNullString

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

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