Package org.kissweb

Class DelimitedFileWriter

java.lang.Object
org.kissweb.DelimitedFileWriter
All Implemented Interfaces:
AutoCloseable

public class DelimitedFileWriter extends Object implements AutoCloseable
Class dealing with the creation of standard comma delimited or CSV files. This class implements the AutoCloseable interface.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a standard CSV file using the standard delimiter (,) and quote (") characters.
    DelimitedFileWriter(String name, boolean append)
    Create or append to a standard CSV file using the standard delimiter (,) and quote (") characters.
    DelimitedFileWriter(String name, boolean append, int itemsPerRow)
    Create or append to a standard CSV file using the standard delimiter (,) and quote (") characters.
    DelimitedFileWriter(String name, boolean append, int itemsPerRow, char delimiter, char quote)
    Create a new or append to an existing CSV file.
    DelimitedFileWriter(String name, char delim)
    Create a new CSV file us ing the specified delimiter character and the default quote character (").
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the CSV file being created.
    void
    End the current row.
    int
    Returns the number of columns output already on the current row.
    void
    Set the format of date output.
    void
    writeDate(int x)
    Output a numeric date in the default format.
    void
    writeField(char x)
    Output a single character field.
    void
    writeField(double x)
    Output a floating point number with a maximum of two decimal places.
    void
    writeField(int x)
    Output an integer column.
    void
    writeField(int charLimit, String x)
    Write a String field limiting the size of the String.
    void
    Write a String field correctly dealing with embedded quotes and delimiters
    void
    Write a String field forcing the string to uppercase and a quoted output.
    void
    writeNoZero(int x)
    Output an integer field using an empty field if the number is zero.

    Methods inherited from class java.lang.Object

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

    • DelimitedFileWriter

      public DelimitedFileWriter(String name, char delim) throws IOException
      Create a new CSV file us ing the specified delimiter character and the default quote character (").
      Parameters:
      name -
      delim -
      Throws:
      IOException
    • DelimitedFileWriter

      public DelimitedFileWriter(String name, boolean append, int itemsPerRow, char delimiter, char quote) throws IOException
      Create a new or append to an existing CSV file.
      Parameters:
      name -
      append -
      itemsPerRow - create a standard number of columns-per-row or specify -1 to indicate that you will explicitly output what is needed
      delimiter -
      quote -
      Throws:
      IOException
    • DelimitedFileWriter

      public DelimitedFileWriter(String name, boolean append, int itemsPerRow) throws IOException
      Create or append to a standard CSV file using the standard delimiter (,) and quote (") characters.
      Parameters:
      name -
      append -
      itemsPerRow - create a standard number of columns-per-row or specify -1 to indicate that you will explicitly output what is needed
      Throws:
      IOException
    • DelimitedFileWriter

      public DelimitedFileWriter(String name, boolean append) throws IOException
      Create or append to a standard CSV file using the standard delimiter (,) and quote (") characters.
      Parameters:
      name -
      append -
      Throws:
      IOException
    • DelimitedFileWriter

      public DelimitedFileWriter(String name) throws IOException
      Create a standard CSV file using the standard delimiter (,) and quote (") characters.
      Parameters:
      name -
      Throws:
      IOException
  • Method Details

    • setDateFormat

      public void setDateFormat(String fmt)
      Set the format of date output. Default is mm/dd/yyyy
      Parameters:
      fmt -
      See Also:
    • close

      public void close()
      Close the CSV file being created. Note that this method will be called automatically if the try-with-resource Java facility is utilized.
      Specified by:
      close in interface AutoCloseable
    • writeField

      public void writeField(String x) throws Exception
      Write a String field correctly dealing with embedded quotes and delimiters
      Parameters:
      x -
      Throws:
      Exception
    • writeField

      public void writeField(int charLimit, String x) throws Exception
      Write a String field limiting the size of the String. Deals correctly with embedded quotes and delimiters.
      Parameters:
      charLimit - max size of the string
      x - the string
      Throws:
      Exception
    • writeFieldUpperCase

      public void writeFieldUpperCase(String x) throws Exception
      Write a String field forcing the string to uppercase and a quoted output.
      Parameters:
      x -
      Throws:
      Exception
    • writeField

      public void writeField(double x) throws Exception
      Output a floating point number with a maximum of two decimal places.
      Parameters:
      x -
      Throws:
      Exception
    • writeField

      public void writeField(int x) throws Exception
      Output an integer column.
      Parameters:
      x -
      Throws:
      Exception
    • writeNoZero

      public void writeNoZero(int x) throws Exception
      Output an integer field using an empty field if the number is zero.
      Parameters:
      x -
      Throws:
      Exception
    • writeField

      public void writeField(char x) throws Exception
      Output a single character field. Quotes and delimiters are correctly handled.
      Parameters:
      x -
      Throws:
      Exception
    • writeDate

      public void writeDate(int x) throws Exception
      Output a numeric date in the default format.
      Parameters:
      x - the date as an integer YYYYMMDD
      Throws:
      Exception
      See Also:
      • dateFmt
    • endRecord

      public void endRecord() throws IOException, Exception
      End the current row. Throws an exception if all of the columns weren't specified.
      Throws:
      IOException
      Exception
    • getFieldCount

      public int getFieldCount()
      Returns the number of columns output already on the current row.
      Returns: