edu.northwestern.at.utils.csv

## Class CSVFileWriter

- java.lang.Object
- - edu.northwestern.at.utils.csv.CSVFile
  - - edu.northwestern.at.utils.csv.CSVFileWriter

-     public class CSVFileWriter
      extends CSVFile

  CSVFileWriter writes text fields to a delimiter separate file.
  Author:
    Fabrizio Fazzino

    Modifications by Philip R. Burns at Northwestern University.

    1. Allow output column selection and ordering.
    2. Allow specification of file character encoding.
    4. Use List instead of Vector.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.io.OutputStreamWriter` | `outputWriter` Print writer for the CSV file contents.

    - ### Fields inherited from class edu.northwestern.at.utils.csv.CSVFile

      `DEFAULT_QUALIFIER, DEFAULT_SEPARATOR, qualifier, separator`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CSVFileWriter(java.lang.String outputFileName, java.lang.String encoding)` Create CSVFileWriter with specified file name and encding,
    `CSVFileWriter(java.lang.String outputFileName, java.lang.String encoding, char separator)` Create CSVFileWriter with specified separator character.
    `CSVFileWriter(java.lang.String outputFileName, java.lang.String encoding, char separator, char qualifier)` CSVFileWriter constructor with given field separator and text qualifier.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Close the output CSV file.
    `java.io.OutputStreamWriter` | `getOutputWriter()` Return output writer.
    `protected void` | `openOutputFile(java.lang.String outputFileName, java.lang.String encoding)` Open output file.
    `void` | `writeEOL()` Output an end of line.
    `void` | `writeFields(java.util.List<?> fields)` Write all fields to a new line in the CSV file.
    `void` | `writeFields(java.util.List<?> fields, int[] indices)` Write selected fields to a new line in the CSV file.
    `void` | `writeFields(java.util.List<?> fields, java.util.List<java.lang.Integer> indices)` Write selected fields to a new line in the CSV file.
    `void` | `writeFields(java.lang.Object[] fields)` Write all fields to a new line in the CSV file.
    `void` | `writeSeparator()` Output a value separator.
    `void` | `writeValue(java.lang.String fieldValue)` Output a value.

    - ### Methods inherited from class edu.northwestern.at.utils.csv.CSVFile

      `getQualifier, getSeparator, setQualifier, setSeparator`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### outputWriter

          protected java.io.OutputStreamWriter outputWriter

      Print writer for the CSV file contents.

  - ### Constructor Detail

    - #### CSVFileWriter

          public CSVFileWriter(java.lang.String outputFileName,
                       java.lang.String encoding)
                        throws java.io.IOException

      Create CSVFileWriter with specified file name and encding,
      Parameters:
        `outputFileName` - Name of CSV file for output.
        `encoding` - Character encoding for file output.
      Throws:
        `java.io.FileNotFoundException` - If file is not found.
        `java.io.IOException` - If an error occurs while writing the file.

    - #### CSVFileWriter

          public CSVFileWriter(java.lang.String outputFileName,
                       java.lang.String encoding,
                       char separator)
                        throws java.io.IOException

      Create CSVFileWriter with specified separator character.
      Parameters:
        `outputFileName` - Name of the CSV file to write to.
        `encoding` - Character encoding for file output.
        `separator` - Field separator character. 8
      Throws:
        `java.io.FileNotFoundException` - If file is not found.
        `java.io.IOException` - If an error occurs while writing the file.

    - #### CSVFileWriter

          public CSVFileWriter(java.lang.String outputFileName,
                       java.lang.String encoding,
                       char separator,
                       char qualifier)
                        throws java.io.FileNotFoundException,
                               java.io.IOException

      CSVFileWriter constructor with given field separator and text qualifier.
      Parameters:
        `outputFileName` - Name of the CSV file to write to.
        `encoding` - Character encoding for file output.
        `separator` - Field separator.
        `qualifier` - Qualifier character. Ascii 0 means no qualifier.
      Throws:
        `java.io.FileNotFoundException` - If file is not found.
        `java.io.IOException` - If an error occurs while writing the file.

  - ### Method Detail

    - #### openOutputFile

          protected void openOutputFile(java.lang.String outputFileName,
                            java.lang.String encoding)
                                 throws java.io.FileNotFoundException,
                                        java.io.IOException

      Open output file.
      Parameters:
        `outputFileName` - Name of the CSV file to write to.
        `encoding` - Character encoding for file output.
      Throws:
        `java.io.FileNotFoundException` - If file is not found.
        `java.io.IOException` - If an error occurs while writing the file.

    - #### close

          public void close()
                     throws java.io.IOException

      Close the output CSV file.
      Throws:
        `java.io.IOException` - If an error occurs while closing the file.

    - #### writeValue

          public void writeValue(java.lang.String fieldValue)
                          throws java.io.IOException

      Output a value.
      Parameters:
        `fieldValue` - The field value to output.
      Throws:
        `java.io.IOException` - If an error occurs while closing the file.

    - #### writeFields

          public void writeFields(java.lang.Object[] fields)
                           throws java.io.IOException

      Write all fields to a new line in the CSV file.
      Parameters:
        `fields` - The array of entries for this line.
      Throws:
        `java.io.IOException`

    - #### writeFields

          public void writeFields(java.util.List<?> fields)
                           throws java.io.IOException

      Write all fields to a new line in the CSV file.
      Parameters:
        `fields` - The list of entries for this line.
      Throws:
        `java.io.IOException`

    - #### writeFields

          public void writeFields(java.util.List<?> fields,
                         java.util.List<java.lang.Integer> indices)
                           throws java.io.IOException

      Write selected fields to a new line in the CSV file.
      Parameters:
        `fields` - The list of entries for this line.
        `indices` - List of integer indices of entries to output.

        The integer "indices" specify which entries to write out, and the order. For example, if "indices" contains the values 3, 4, 1 then entries 3, 4, and 1 from "fields" are written out in that order. Bad indices result in an empty field being output.

      Throws:
        `java.io.IOException`

    - #### writeFields

          public void writeFields(java.util.List<?> fields,
                         int[] indices)
                           throws java.io.IOException

      Write selected fields to a new line in the CSV file.
      Parameters:
        `fields` - The list of entries for this line.
        `indices` - Array of integer indices of entries to output.

        The integer "indices" specify which entries to write out, and the order. For example, if "indices" contains the values 3, 4, 1 then entries 3, 4, and 1 from "fields" are written out in that order. Bad indices result in an empty field being output.

      Throws:
        `java.io.IOException`

    - #### writeEOL

          public void writeEOL()
                        throws java.io.IOException

      Output an end of line.
      Throws:
        `java.io.IOException` - If an error occurs while closing the file.

    - #### writeSeparator

          public void writeSeparator()
                              throws java.io.IOException

      Output a value separator.
      Throws:
        `java.io.IOException` - If an error occurs while closing the file.

    - #### getOutputWriter

          public java.io.OutputStreamWriter getOutputWriter()

      Return output writer.
      Returns:
        The output writer.