Class DelimitedWriter


  • class DelimitedWriter
    extends java.lang.Object
    Helper class for writing out CSV or tab delimited files.

    Example Usage:

     delimitedWriter.writeFields("header1", "header2", ...);
     for each line to be written {
       delimitedWriter.writeField(value1);
       delimitedWriter.writeField(value2);
       delimitedWriter.nextLine();
     }
     delimitedWriter.close();
     

    • Constructor Summary

      Constructors 
      Constructor Description
      DelimitedWriter​(java.io.Writer delegate)
      Creates a new Delimited writer using the default delimiter
      DelimitedWriter​(java.io.Writer delegate, char delimiter)
      Creates a new Delimited writer using the default delimiter
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the underlying writer object.
      private java.lang.String escape​(java.lang.String value)
      Escapes any occurrences of the quote character in value by replacing it with a double quote.
      void nextLine()
      Output a new line and advance the writer to the next line.
      void write​(int value)
      Write a single integer value.
      void write​(int... values)
      Write muliple integer values
      void write​(java.lang.String field)
      Write a single value.
      void write​(java.lang.String... fields)
      Write multiple fields at once.
      • Methods inherited from class java.lang.Object

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

      • NEW_LINE

        private static final java.lang.String NEW_LINE
      • delimiter

        private final char delimiter
      • delegate

        private final java.io.Writer delegate
      • fieldPosition

        private int fieldPosition
    • Constructor Detail

      • DelimitedWriter

        public DelimitedWriter​(java.io.Writer delegate)
        Creates a new Delimited writer using the default delimiter
        Parameters:
        delegate - Writer to delegate all writes to
      • DelimitedWriter

        public DelimitedWriter​(java.io.Writer delegate,
                               char delimiter)
        Creates a new Delimited writer using the default delimiter
        Parameters:
        delegate - Writer to delegate all writes to
        delimiter - delimiter to use (usually a comma, tab or space)
    • Method Detail

      • write

        public void write​(java.lang.String... fields)
                   throws java.io.IOException
        Write multiple fields at once. Values will be auto escaped and quoted as needed. Each value will be separated using the current delimiter
        Parameters:
        fields - Values to write
        Throws:
        java.io.IOException - Error writing to the underlying writer object
      • write

        public void write​(java.lang.String field)
                   throws java.io.IOException
        Write a single value. Values will be auto escaped and quoted as needed. If this is not the first field of the current line the value will be prepended with the current delimiter
        Parameters:
        field - Value to write
        Throws:
        java.io.IOException - Error writing to the underlying writer object
      • write

        public void write​(int value)
                   throws java.io.IOException
        Write a single integer value.
        Parameters:
        value - Value to write
        Throws:
        java.io.IOException - Error writing to the underlying writer object
      • write

        public void write​(int... values)
                   throws java.io.IOException
        Write muliple integer values
        Parameters:
        values - values to write
        Throws:
        java.io.IOException - Error writing to the underlying writer object
      • nextLine

        public void nextLine()
                      throws java.io.IOException
        Output a new line and advance the writer to the next line. The line delimiter is the default for the platform.
        Throws:
        java.io.IOException - Error writing to the underlying writer object
      • close

        public void close()
                   throws java.io.IOException
        Close the underlying writer object. Once closed all write operations will fail
        Throws:
        java.io.IOException - Error closing the underlying writer object
      • escape

        private java.lang.String escape​(java.lang.String value)
        Escapes any occurrences of the quote character in value by replacing it with a double quote. Also Quotes the value if a quote or delimiter value is found.
        Parameters:
        value - String that needs escaping
        Returns:
        New string with all values escaped