liquibase.util.csv.opencsv
Class CSVWriter

java.lang.Object
  extended by liquibase.util.csv.opencsv.CSVWriter
All Implemented Interfaces:
Closeable, Flushable
Direct Known Subclasses:
CSVWriter

public class CSVWriter
extends Object
implements Closeable, Flushable

A very simple CSV writer released under a commercial-friendly license.

Author:
Glen Smith

Field Summary
static char DEFAULT_ESCAPE_CHARACTER
          The character used for escaping quotes.
static String DEFAULT_LINE_END
          Default line terminator.
static char DEFAULT_QUOTE_CHARACTER
          The default quote character to use if none is supplied to the constructor.
static char DEFAULT_SEPARATOR
          The default separator to use if none is supplied to the constructor.
static int INITIAL_STRING_SIZE
           
static char NO_ESCAPE_CHARACTER
          The escape constant to use when you wish to suppress all escaping.
static char NO_QUOTE_CHARACTER
          The quote constant to use when you wish to suppress all quoting.
static String RFC4180_LINE_END
          RFC 4180 compliant line terminator.
 
Constructor Summary
CSVWriter(Writer writer)
          Constructs CSVWriter using a comma for the separator.
CSVWriter(Writer writer, char separator)
          Constructs CSVWriter with supplied separator.
CSVWriter(Writer writer, char separator, char quotechar)
          Constructs CSVWriter with supplied separator and quote char.
CSVWriter(Writer writer, char separator, char quotechar, char escapechar)
          Constructs CSVWriter with supplied separator and quote char.
CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd)
          Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
CSVWriter(Writer writer, char separator, char quotechar, String lineEnd)
          Constructs CSVWriter with supplied separator and quote char.
 
Method Summary
 boolean checkError()
          Checks to see if the there has been an error in the printstream.
 void close()
          Close the underlying stream writer flushing any buffered content.
 void flush()
          Flush underlying stream to writer.
 void flushQuietly()
          flushes the writer without throwing any exceptions.
protected  StringBuilder processLine(String nextElement)
          Processes all the characters in a line.
protected  boolean stringContainsSpecialCharacters(String line)
          checks to see if the line contains special characters.
 void writeAll(List<String[]> allLines)
          Writes the entire list to a CSV file.
 void writeAll(List<String[]> allLines, boolean applyQuotesToAll)
          Writes the entire list to a CSV file.
 void writeNext(String[] nextLine)
          Writes the next line to the file.
 void writeNext(String[] nextLine, boolean applyQuotesToAll)
          Writes the next line to the file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INITIAL_STRING_SIZE

public static final int INITIAL_STRING_SIZE
See Also:
Constant Field Values

DEFAULT_ESCAPE_CHARACTER

public static final char DEFAULT_ESCAPE_CHARACTER
The character used for escaping quotes.

See Also:
Constant Field Values

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
The default separator to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_QUOTE_CHARACTER

public static final char DEFAULT_QUOTE_CHARACTER
The default quote character to use if none is supplied to the constructor.

See Also:
Constant Field Values

NO_QUOTE_CHARACTER

public static final char NO_QUOTE_CHARACTER
The quote constant to use when you wish to suppress all quoting.

See Also:
Constant Field Values

NO_ESCAPE_CHARACTER

public static final char NO_ESCAPE_CHARACTER
The escape constant to use when you wish to suppress all escaping.

See Also:
Constant Field Values

DEFAULT_LINE_END

public static final String DEFAULT_LINE_END
Default line terminator.

See Also:
Constant Field Values

RFC4180_LINE_END

public static final String RFC4180_LINE_END
RFC 4180 compliant line terminator.

See Also:
Constant Field Values
Constructor Detail

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)
Constructs CSVWriter with supplied separator.

Parameters:
writer - the writer to an underlying CSV source.
separator - the delimiter to use for separating entries.

CSVWriter

public CSVWriter(Writer writer,
                 char separator,
                 char quotechar)
Constructs CSVWriter with supplied separator and quote char.

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

CSVWriter

public CSVWriter(Writer writer,
                 char separator,
                 char quotechar,
                 char escapechar)
Constructs CSVWriter with supplied separator and quote char.

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

CSVWriter

public CSVWriter(Writer writer,
                 char separator,
                 char quotechar,
                 String lineEnd)
Constructs CSVWriter with supplied separator and quote char.

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
lineEnd - the line feed terminator to use

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 Detail

writeAll

public void writeAll(List<String[]> allLines,
                     boolean applyQuotesToAll)
Writes the entire list to a CSV file. The list is assumed to be a String[]

Parameters:
allLines - a List of String[], with each String[] representing a line of the file.
applyQuotesToAll - 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(List<String[]> allLines)
Writes the entire list to a CSV file. The list is assumed to be a String[]

Parameters:
allLines - a List of String[], with each String[] representing a line of the file.

writeNext

public void writeNext(String[] nextLine,
                      boolean applyQuotesToAll)
Writes the next line to the file.

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.

writeNext

public void writeNext(String[] nextLine)
Writes the next line to the file.

Parameters:
nextLine - a string array with each comma-separated element as a separate entry.

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 StringBuilder processLine(String nextElement)
Processes all the characters in a line.

Parameters:
nextElement - - 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 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.

flushQuietly

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



Copyright © 2016 Liquibase.org. All rights reserved.