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.
      • rowSeparator

        protected String rowSeparator
        Separator between rows (default: LF)
    • 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​(Writer writer)
        CSVFileWriter constructor with default values.
        Parameters:
        writer - The Writer to be opened for writing
      • CSVFileWriter

        public CSVFileWriter​(OutputStream stream)
        CSVFileWriter constructor with default values. The CSV file will be written in UTF-8 encoding.
        Parameters:
        stream - The OutputStream to be opened for writing
      • 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​(OutputStream stream,
                             char sep,
                             char qual)
        CSVFileWriter constructor with given field separator and text qualifier. The CSV file will be written in UTF-8 encoding.
        Parameters:
        stream - The OutputStream 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
      • 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

      • getRowSeparator

        public String getRowSeparator()
        Gets the current row separator
        Returns:
        row separator (CR, LF or CRLF) as String
      • setRowSeparator

        public void setRowSeparator​(String separator)
        Sets the row separator to an arbitrary string. However, setting it to anything other than CR, LF, or a combination of these will be ignored by the field escape code, and is therefore undefined. Setting it to System.lineSeparator() might be sensible.
        Parameters:
        separator - to use (default: "\n")
      • close

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

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