jhplot.io.csv
Class CSVWriter
- java.lang.Object
-
- jhplot.io.csv.CSVWriter
-
- All Implemented Interfaces:
- java.io.Closeable, java.lang.AutoCloseable
public class CSVWriter extends java.lang.Object implements java.io.Closeable
A very simple CSV writer released under a commercial-friendly license.
-
-
Field Summary
Fields Modifier and Type Field and Description static char
DEFAULT_ESCAPE_CHARACTER
The character used for escaping quotes.static java.lang.String
DEFAULT_LINE_END
Default line terminator uses platform encoding.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.
-
Constructor Summary
Constructors Constructor and Description CSVWriter(java.io.File file, char separator)
Constructs CSVWriter with supplied separator.CSVWriter(java.io.File file, char separator, char quotechar)
Constructs CSVWriter with supplied separator and quote char.CSVWriter(java.lang.String file, char separator)
Constructs CSVWriter with supplied separator.CSVWriter(java.lang.String file, char separator, char quotechar)
Constructs CSVWriter with supplied separator and quote char.CSVWriter(java.lang.String file, char separator, char quotechar, char escapechar)
Constructs CSVWriter with supplied separator and quote char.CSVWriter(java.io.Writer writer)
Constructs CSVWriter using a comma for the separator.CSVWriter(java.io.Writer writer, char separator)
Constructs CSVWriter with supplied separator.CSVWriter(java.io.Writer writer, char separator, char quotechar)
Constructs CSVWriter with supplied separator and quote char.CSVWriter(java.io.Writer writer, char separator, char quotechar, char escapechar)
Constructs CSVWriter with supplied separator and quote char.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.CSVWriter(java.io.Writer writer, char separator, char quotechar, java.lang.String lineEnd)
Constructs CSVWriter with supplied separator and quote char.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description 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
setResultService(ResultSetHelper resultService)
void
write(java.lang.String[] nextLine)
Writes the next line to the file.void
writeAll(java.util.List<java.lang.String[]> allLines)
Writes the entire list to a CSV file.void
writeAll(java.sql.ResultSet rs, boolean includeColumnNames)
Writes the entire ResultSet to a CSV file.void
writeNext(java.lang.String[] nextLine)
Writes the next line to the file.
-
-
-
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 java.lang.String DEFAULT_LINE_END
Default line terminator uses platform encoding.- See Also:
- Constant Field Values
-
-
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)
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(java.lang.String file, char separator) throws java.io.IOException
Constructs CSVWriter with supplied separator.- Parameters:
file
- full path to the file.separator
- the delimiter to use for separating entries.- Throws:
java.io.IOException
-
CSVWriter
public CSVWriter(java.io.File file, char separator) throws java.io.IOException
Constructs CSVWriter with supplied separator.- Parameters:
file
- full path to the file.separator
- the delimiter to use for separating entries.- Throws:
java.io.IOException
-
CSVWriter
public CSVWriter(java.io.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 entriesquotechar
- the character to use for quoted elements
-
CSVWriter
public CSVWriter(java.io.File file, char separator, char quotechar) throws java.io.IOException
Constructs CSVWriter with supplied separator and quote char.- Parameters:
file
- output file for an underlying CSV source.separator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elements- Throws:
java.io.IOException
-
CSVWriter
public CSVWriter(java.lang.String file, char separator, char quotechar) throws java.io.IOException
Constructs CSVWriter with supplied separator and quote char.- Parameters:
file
- output file for an underlying CSV source.separator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elements- Throws:
java.io.IOException
-
CSVWriter
public CSVWriter(java.lang.String file, char separator, char quotechar, char escapechar) throws java.io.IOException
Constructs CSVWriter with supplied separator and quote char.- Parameters:
file
- output file for an underlying CSV source.separator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementsescapechar
- the character to use for escaping quotechars or escapechars- Throws:
java.io.IOException
-
CSVWriter
public CSVWriter(java.io.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 entriesquotechar
- the character to use for quoted elementsescapechar
- the character to use for escaping quotechars or escapechars
-
CSVWriter
public CSVWriter(java.io.Writer writer, char separator, char quotechar, java.lang.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 entriesquotechar
- the character to use for quoted elementslineEnd
- the line feed terminator to use
-
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 entriesquotechar
- the character to use for quoted elementsescapechar
- the character to use for escaping quotechars or escapecharslineEnd
- the line feed terminator to use
-
-
Method Detail
-
writeAll
public void writeAll(java.util.List<java.lang.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.
-
writeAll
public void writeAll(java.sql.ResultSet rs, boolean includeColumnNames) throws java.sql.SQLException, java.io.IOException
Writes the entire ResultSet to a CSV file. The caller is responsible for closing the ResultSet.- Parameters:
rs
- the recordset to writeincludeColumnNames
- true if you want column names in the output, false otherwise- Throws:
java.io.IOException
- thrown by getColumnValuejava.sql.SQLException
- thrown by getColumnValue
-
write
public void write(java.lang.String[] nextLine)
Writes the next line to the file. Same as writeNext().- Parameters:
nextLine
- a string array with each comma-separated element as a separate entry.
-
writeNext
public void writeNext(java.lang.String[] nextLine)
Writes the next line to the file.- Parameters:
nextLine
- a string array with each comma-separated element as a separate entry.
-
flush
public void flush() throws java.io.IOException
Flush underlying stream to writer.- Throws:
java.io.IOException
- if bad things happen
-
close
public void close() throws java.io.IOException
Close the underlying stream writer flushing any buffered content.- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Throws:
java.io.IOException
- if bad things happen
-
checkError
public boolean checkError()
Checks to see if the there has been an error in the printstream.
-
setResultService
public void setResultService(ResultSetHelper resultService)
-
-
DMelt 3.0 © DataMelt by jWork.ORG