Class CSVFileWriter

  • Direct Known Subclasses:
    SSVFileWriter

    public class CSVFileWriter
    extends CSVFile
    CSVFileWriter is a class derived from CSVFile used to format some fields into a new CSV file. As of CSVFile 2.0, this will handle escaping fields whose contents contain the field separator, text qualifier, carriage return and/or line feed properly.
    Author:
    Fabrizio Fazzino
    • Field Detail

      • out

        protected final PrintWriter out
        The print writer linked to the CSV file to be written.
    • Constructor Detail

      • CSVFileWriter

        public CSVFileWriter​(String outputFileName)
                      throws IOException
        CSVFileWriter constructor just needing the name of the CSV file that will be written.
        Parameters:
        outputFileName - The name of the CSV file to be opened for writing
        Throws:
        IOException - if an error occurs while creating the file
      • CSVFileWriter

        public CSVFileWriter​(String outputFileName,
                             char sep)
                      throws IOException
        CSVFileWriter constructor with a given field separator.
        Parameters:
        outputFileName - The name of the CSV file to be opened for writing
        sep - The field separator to be used; overwrites the default one
        Throws:
        IOException - if an error occurs while creating the file
      • CSVFileWriter

        public CSVFileWriter​(String outputFileName,
                             char sep,
                             char qual)
                      throws IOException
        CSVFileWriter constructor with given field separator and text qualifier.
        Parameters:
        outputFileName - The name of the CSV file to be opened for writing
        sep - The field separator to be used; overwrites the default one
        qual - The text qualifier to be used; overwrites the default one
        Throws:
        IOException - if an error occurs while creating the file
      • CSVFileWriter

        public CSVFileWriter​(Writer writer,
                             char sep,
                             char qual)
        CSVFileWriter constructor with given field separator and text qualifier.
        Parameters:
        writer - The Writer to be opened for writing
        sep - The field separator to be used; overwrites the default one
        qual - The text qualifier to be used; overwrites the default one
    • Method Detail

      • close

        public void close()
        Closes the output CSV file.
      • writeFields

        public void writeFields​(List<?> fields)
        Joins the fields and writes them as a new line to the CSV file.
        Parameters:
        fields - The list of strings containing the fields
      • emitRowSeparator

        protected void emitRowSeparator()
        Called after a list of fields has been output.
      • prepareField

        protected String prepareField​(Object field)
        Prepares a field for output by stringifying the passed object and quoting it.
        Parameters:
        field - to prepare
        Returns:
        quoted string