Package com.opencsv

Class CSVWriter

All Implemented Interfaces:
ICSVWriter, Closeable, Flushable, AutoCloseable

public class CSVWriter extends AbstractCSVWriter
A very simple CSV writer released under a commercial-friendly license.
  • Field Details

    • separator

      protected final char separator
    • quotechar

      protected final char quotechar
    • escapechar

      protected final char escapechar
  • Constructor Details

    • CSVWriter

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

      public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, 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 Details

    • writeNext

      protected void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws 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:
      IOException - Exceptions thrown by the writer supplied to CSVWriter.
    • appendQuoteCharacterIfNeeded

      private void appendQuoteCharacterIfNeeded(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters) throws IOException
      Throws:
      IOException
    • stringContainsSpecialCharacters

      protected boolean stringContainsSpecialCharacters(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(String nextElement, Appendable appendable) throws IOException
      Processes all the characters in a line.
      Parameters:
      nextElement - Element to process.
      appendable - - Appendable holding the processed data.
      Throws:
      IOException - - IOException thrown by the writer supplied to the CSVWriter
    • processCharacter

      protected void processCharacter(Appendable appendable, char nextChar) throws 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:
      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