Class CSVWriter

java.lang.Object
com.helger.commons.csv.CSVWriter
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class CSVWriter extends Object implements Closeable, Flushable
A very simple CSV writer released under a commercial-friendly license.
Author:
Glen Smith, Philip Helger
  • Field Details

    • NO_QUOTE_CHARACTER

      public static final char NO_QUOTE_CHARACTER
      The quote constant to use when you wish to suppress all quoting.
      See Also:
    • NO_ESCAPE_CHARACTER

      public static final char NO_ESCAPE_CHARACTER
      The escape constant to use when you wish to suppress all escaping.
      See Also:
    • DEFAULT_LINE_END

      public static final String DEFAULT_LINE_END
      The default line delimiter to be used.
      See Also:
    • DEFAULT_QUOTE_ALL

      public static final boolean DEFAULT_QUOTE_ALL
      By default all text fields are always quoted.
      See Also:
    • DEFAULT_AVOID_FINAL_LINE_END

      public static final boolean DEFAULT_AVOID_FINAL_LINE_END
      By default the file ends with a new-line.
      See Also:
  • Constructor Details

  • Method Details

    • getSeparatorChar

      public char getSeparatorChar()
      Returns:
      The default separator for this writer.
    • setSeparatorChar

      @Nonnull public CSVWriter setSeparatorChar(char cSeparator)
      Sets the delimiter to use for separating entries.
      Parameters:
      cSeparator - the delimiter to use for separating entries
      Returns:
      this
    • getQuoteChar

      public char getQuoteChar()
      Returns:
      The default quotation character for this writer.
    • setQuoteChar

      @Nonnull public CSVWriter setQuoteChar(char cQuoteChar)
      Sets the character to use for quoted elements.
      Parameters:
      cQuoteChar - the character to use for quoted element.
      Returns:
      this
    • getEscapeChar

      public char getEscapeChar()
      Returns:
      The default escape character for this writer.
    • setEscapeChar

      @Nonnull public CSVWriter setEscapeChar(char cEscapeChar)
      Sets the character to use for escaping a separator or quote.
      Parameters:
      cEscapeChar - the character to use for escaping a separator or quote.
      Returns:
      this
    • getLineEnd

      @Nonnull @Nonempty public String getLineEnd()
      Returns:
      the line delimiting string. Neither null nor empty.
    • setLineEnd

      @Nonnull public CSVWriter setLineEnd(@Nonnull @Nonempty String sLineEnd)
      Set the line delimiting string.
      Parameters:
      sLineEnd - The line end. May neither be null nor empty.
      Returns:
      this
    • isAvoidFinalLineEnd

      public boolean isAvoidFinalLineEnd()
      Returns:
      true if the file should not end with a new-line, false otherwise.
      Since:
      8.6.6
    • setAvoidFinalLineEnd

      @Nonnull public CSVWriter setAvoidFinalLineEnd(boolean bAvoidFinalLineEnd)
      Set whether the CSV file should end with a new line or not.
      Parameters:
      bAvoidFinalLineEnd - true to avoid the CSV file ending with a new line.
      Returns:
      this for chaining
      Since:
      8.6.6
    • isApplyQuotesToAll

      public boolean isApplyQuotesToAll()
      Returns:
      true if all cells should always be quoted, false otherwise. The default is true.
      Since:
      8.6.6
    • setApplyQuotesToAll

      @Nonnull public CSVWriter setApplyQuotesToAll(boolean bApplyQuotesToAll)
      Set whether all cells should be quoted by default or not.
      Parameters:
      bApplyQuotesToAll - true to quote all cells, false to quote only the ones where it is necessary
      Returns:
      this for chaining
      Since:
      8.6.6
    • writeAll

      public void writeAll(@Nonnull List<? extends List<String>> aAllLines, boolean bApplyQuotesToAll)
      Writes the entire list to a CSV file.
      Parameters:
      aAllLines - a List of List of String, with each List of String representing a line of the file.
      bApplyQuotesToAll - true if all values are to be quoted. false if quotes only to be applied to values which contain the separator, escape, quote or new line characters.
    • writeAll

      public void writeAll(@Nonnull List<? extends List<String>> aAllLines)
      Writes the entire list to a CSV file using the default quoting setting.
      Parameters:
      aAllLines - a List of List of String, with each List of String representing a line of the file.
      See Also:
    • writeNext

      public void writeNext(@Nullable Iterator<String> aNextLine, boolean bApplyQuotesToAll)
      Writes the next line to the file.
      Parameters:
      aNextLine - A collection of Strings where each entry represents a single cell.
      bApplyQuotesToAll - true if all values are to be quoted. false applies quotes only to values which contain the separator, escape, quote or new line characters.
    • writeNext

      public void writeNext(@Nullable Iterable<String> aNextLine, boolean bApplyQuotesToAll)
      Writes the next line to the file.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      bApplyQuotesToAll - true if all values are to be quoted. false applies quotes only to values which contain the separator, escape, quote or new line characters.
    • writeNext

      public void writeNext(@Nullable Iterable<String> aNextLine)
      Writes the next line to the file using the default quoting settings.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      See Also:
    • writeNext

      public void writeNext(@Nullable String[] aNextLine, boolean bApplyQuotesToAll)
      Writes the next line to the file.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      bApplyQuotesToAll - true if all values are to be quoted. false applies quotes only to values which contain the separator, escape, quote or new line characters.
    • writeNext

      public void writeNext(@Nullable String... aNextLine)
      Writes the next line to the file using the default quoting settings.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      See Also:
    • writeNext

      public void writeNext(@Nullable String[] aNextLine, @Nonnegative int nOfs, @Nonnegative int nLength, boolean bApplyQuotesToAll)
      Writes the next line to the file.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      nOfs - Array Offset. Must be ≥ 0.
      nLength - Array Length. Must be ≥ 0.
      bApplyQuotesToAll - true if all values are to be quoted. false applies quotes only to values which contain the separator, escape, quote or new line characters.
    • writeNext

      public void writeNext(@Nullable String[] aNextLine, @Nonnegative int nOfs, @Nonnegative int nLength)
      Writes the next line to the file using the default quoting setting.
      Parameters:
      aNextLine - a string array with each comma-separated element as a separate entry.
      nOfs - Array Offset. Must be ≥ 0.
      nLength - Array Length. Must be ≥ 0.
      See Also:
    • stringContainsSpecialCharacters

      protected boolean stringContainsSpecialCharacters(@Nonnull String sLine)
      checks to see if the line contains special characters.
      Parameters:
      sLine - - element of data to check for special characters.
      Returns:
      true if the line contains the quote, escape, separator, newline or return.
    • getEscapedText

      @Nonnull protected StringBuilder getEscapedText(@Nonnull String sNextElement)
      Processes all the characters in a line.
      Parameters:
      sNextElement - element to process.
      Returns:
      a StringBuilder with the elements data.
    • flush

      public void flush() throws IOException
      Flush underlying stream to writer.
      Specified by:
      flush in interface Flushable
      Throws:
      IOException - if bad things happen
    • close

      public void close() throws IOException
      Close the underlying stream writer flushing any buffered content.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if bad things happen
    • checkError

      public boolean checkError()
      Checks to see if the there has been an error in the printstream.
      Returns:
      true if the print stream has encountered an error, either on the underlying output stream or during a format conversion.
    • getWrittenLines

      @Nonnegative public int getWrittenLines()
      Returns:
      The number of written lines. Always ≥ 0.
      Since:
      8.6.6
    • flushQuietly

      public void flushQuietly()
      flushes the writer without throwing any exceptions.