public class CsvWriter
extends java.lang.Object
Constructor and Description |
---|
CsvWriter(java.lang.String filePath)
Constructs CSVWriter with the given filePath as the output file
|
CsvWriter(java.lang.String filePath,
boolean addTimeStamp)
Constructs CSVWriter with the given filePath If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')
|
CsvWriter(java.lang.String filePath,
char separator)
Constructs CSVWriter with the given filePath and separator.
|
CsvWriter(java.lang.String filePath,
char separator,
boolean addTimeStamp)
Constructs CSVWriter with the given filePath and separator If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')
|
CsvWriter(java.lang.String filePath,
char separator,
char quotechar)
Constructs CSVWriter with the given filePath, separator and quotchar
|
CsvWriter(java.lang.String filePath,
char separator,
char quotechar,
char escapechar)
Constructs CSVWriter with the given filePath, separator, quote character and escape character
|
CsvWriter(java.lang.String filePath,
char separator,
char quotechar,
char escapechar,
java.lang.String lineEnd)
Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
|
CsvWriter(java.lang.String filePath,
char separator,
char quotechar,
java.lang.String lineEnd)
Constructs CSVWriter with the given filePath, separator, quote character, escape character and line end character
|
CsvWriter(java.io.Writer writer)
Constructs CSVWriter with the given writer
|
CsvWriter(java.io.Writer writer,
char separator)
Constructs CSVWriter with the given filePath and separator.
|
CsvWriter(java.io.Writer writer,
char separator,
char quotechar)
Constructs CSVWriter with the given filePath, separator and quotchar
|
CsvWriter(java.io.Writer writer,
char separator,
char quotechar,
char escapechar)
Constructs CSVWriter with the given filePath, separator, quote character and escape character
|
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 the given filePath, separator, quote character, escape character and line end character
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Close the underlying stream writer flushing any buffered content.
|
void |
flush()
Flush underlying stream to writer.
|
java.lang.String |
getCsvPath()
Returns the path of the file being written to.
|
java.lang.String[] |
getHeaderRow()
Returns the header row
|
java.lang.String[] |
getMappingLabels()
Returns the mapping labels
|
void |
setAutoCloseAfterScrapeEnds(ScrapingSession session)
Tells the writer to close when the scrape ends.
|
void |
setHeader(java.lang.String[] header)
Sets the header row to the given String[] and also sets mapping Values to the same String[] The header is only written if the file doesn't originally exist when writing to
it.
|
void |
setMapValues(java.lang.String[] mapVals)
Set the values used to map the key values of a Map<String, String> into CSV rows.
|
void |
setMapValuesAndLowercaseUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to lowercase and underscores for spaces eg.
|
void |
setMapValuesAndUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to have underscores for spaces eg.
|
void |
setMapValuesAndUppercaseUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to uppercase and underscores for spaces eg.
|
java.lang.String |
toString() |
void |
write(java.util.List nextLine)
Writes the next line to the file.
|
void |
write(java.util.Map nextLine)
Writes the next line to the file.
|
void |
write(java.lang.String[] nextLine)
Writes the next line to the file.
|
void |
writeAll(DataSet set)
Writes all the dataRecords in the set to the CSV (The map values must be set for this to work)
|
void |
writeAll(java.util.List lines)
Writes the entire list to a CSV file.
|
void |
writeAll(java.sql.ResultSet rs,
boolean includeColumnNames)
Writes the entire ResultSet to a CSV file.
|
public CsvWriter(@NotNull java.lang.String filePath) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.java.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.separator
- the delimiter to use for separating entries.java.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, boolean addTimeStamp) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.addTimeStamp
- whether to add a current time stamp to the filenamejava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator, boolean addTimeStamp) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.separator
- the delimiter to use for separating entries.addTimeStamp
- whether to add a current time stamp to the filenamejava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator, char quotechar) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.separator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementsjava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator, char quotechar, char escapechar) throws java.io.IOException
filePath
- the filepath for the 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 escapecharsjava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator, char quotechar, java.lang.String lineEnd) throws java.io.IOException
filePath
- the filepath for the underlying CSV source.separator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementslineEnd
- the line feed terminator to usejava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.lang.String filePath, char separator, char quotechar, char escapechar, java.lang.String lineEnd) throws java.io.IOException
filePath
- the filepath for the 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 usejava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.io.Writer writer) throws java.io.IOException
writer
- The writer to write tojava.io.IOException
- On error preparing to writepublic CsvWriter(@NotNull java.io.Writer writer, char separator) throws java.io.IOException
writer
- The writer to write toseparator
- the delimiter to use for separating entries.java.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.io.Writer writer, char separator, char quotechar) throws java.io.IOException
writer
- The writer to write toseparator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementsjava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.io.Writer writer, char separator, char quotechar, char escapechar) throws java.io.IOException
writer
- The writer to write toseparator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementsescapechar
- the character to use for escaping quotechars or escapecharsjava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.io.Writer writer, char separator, char quotechar, java.lang.String lineEnd) throws java.io.IOException
writer
- The writer to write toseparator
- the delimiter to use for separating entriesquotechar
- the character to use for quoted elementslineEnd
- the line feed terminator to usejava.io.IOException
- If the specified file could not be foundpublic CsvWriter(@NotNull java.io.Writer writer, char separator, char quotechar, char escapechar, java.lang.String lineEnd) throws java.io.IOException
writer
- The writer to write toseparator
- 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 usejava.io.IOException
- If the specified file could not be foundpublic void setHeader(@NotNull java.lang.String[] header)
header
- The headers to usepublic void setMapValues(java.lang.String[] mapVals)
mapVals
- The values to use for each header key (in order) when reading values from a mappublic void setMapValuesAndUppercaseUnderscore(@NotNull java.lang.String[] mapVals)
mapVals
- The values to use for each header key (in order) when reading values from a mappublic void setMapValuesAndLowercaseUnderscore(@NotNull java.lang.String[] mapVals)
mapVals
- The values to use for each header key (in order) when reading values from a mappublic void setMapValuesAndUnderscore(@NotNull java.lang.String[] mapVals)
mapVals
- The values to use for each header key (in order) when reading values from a mappublic void writeAll(@NotNull java.util.List lines)
lines
- a List of String[] or List<String>, each item representing a line of the file.public void writeAll(@NotNull DataSet set)
set
- The DataSet of records to writepublic void write(@NotNull java.util.List nextLine)
nextLine
- a list of Strings with each comma-separated element as a separate entry.public void write(@NotNull java.util.Map nextLine)
nextLine
- a map of String => Stringpublic void write(java.lang.String[] nextLine)
nextLine
- a string array with each comma-separated element as a separate entry.public void flush() throws java.io.IOException
java.io.IOException
- if bad things happenpublic void close() throws java.io.IOException
java.io.IOException
- if bad things happenpublic void setAutoCloseAfterScrapeEnds(@NotNull ScrapingSession session)
session
- The current sessionpublic void writeAll(@NotNull java.sql.ResultSet rs, boolean includeColumnNames) throws java.sql.SQLException, java.io.IOException
rs
- the result set to writeincludeColumnNames
- true if you want column names in the output, false otherwisejava.sql.SQLException
- If the result set cannot be readjava.io.IOException
- On error writing the data to file@Nullable public java.lang.String getCsvPath()
@Nullable public java.lang.String[] getHeaderRow()
@Nullable public java.lang.String[] getMappingLabels()
@NotNull public java.lang.String toString()
toString
in class java.lang.Object