Package com.opencsv

Class CSVWriter

  • All Implemented Interfaces:
    ICSVWriter, java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public class CSVWriter
    extends AbstractCSVWriter
    A very simple CSV writer released under a commercial-friendly license.
    Author:
    Glen Smith
    • Field Detail

      • separator

        protected final char separator
      • quotechar

        protected final char quotechar
      • escapechar

        protected final char escapechar
    • Constructor Detail

      • CSVWriter

        public CSVWriter​(java.io.Writer writer)
        Constructs CSVWriter using a comma for the separator.
        Parameters:
        writer - The writer to an underlying CSV source.
      • CSVWriter

        public CSVWriter​(java.io.Writer writer,
                         char separator,
                         char quotechar,
                         char escapechar,
                         java.lang.String lineEnd)
        Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
        Parameters:
        writer - The writer to an underlying CSV source.
        separator - The delimiter to use for separating entries
        quotechar - The character to use for quoted elements
        escapechar - The character to use for escaping quotechars or escapechars
        lineEnd - The line feed terminator to use
    • Method Detail

      • writeNext

        protected void writeNext​(java.lang.String[] nextLine,
                                 boolean applyQuotesToAll,
                                 java.lang.Appendable appendable)
                          throws java.io.IOException
        Description copied from class: AbstractCSVWriter
        Writes the next line to the file. This method is a fail-fast method that will throw the IOException of the writer supplied to the CSVWriter (if the Writer does not handle the exceptions itself like the PrintWriter class).
        Specified by:
        writeNext in class AbstractCSVWriter
        Parameters:
        nextLine - a string array with each comma-separated element as a separate entry.
        applyQuotesToAll - true if all values are to be quoted. false applies quotes only to values which contain the separator, escape, quote or new line characters.
        appendable - Appendable used as buffer.
        Throws:
        java.io.IOException - Exceptions thrown by the writer supplied to CSVWriter.
      • stringContainsSpecialCharacters

        protected boolean stringContainsSpecialCharacters​(java.lang.String line)
        Checks to see if the line contains special characters.
        Parameters:
        line - Element of data to check for special characters.
        Returns:
        True if the line contains the quote, escape, separator, newline, or return.
      • processLine

        protected void processLine​(java.lang.String nextElement,
                                   java.lang.Appendable appendable)
                            throws java.io.IOException
        Processes all the characters in a line.
        Parameters:
        nextElement - Element to process.
        appendable - - Appendable holding the processed data.
        Throws:
        java.io.IOException - - IOException thrown by the writer supplied to the CSVWriter
      • processCharacter

        protected void processCharacter​(java.lang.Appendable appendable,
                                        char nextChar)
                                 throws java.io.IOException
        Appends the character to the StringBuilder adding the escape character if needed.
        Parameters:
        appendable - - Appendable holding the processed data.
        nextChar - Character to process
        Throws:
        java.io.IOException - - IOException thrown by the writer supplied to the CSVWriter.
      • checkCharactersToEscape

        protected boolean checkCharactersToEscape​(char nextChar)
        Checks whether the next character that is to be written out is a special character that must be quoted. The quote character, escape charater, and separator are special characters.
        Parameters:
        nextChar - The next character to be written
        Returns:
        Whether the character needs to be quoted or not