Package org.evolvis.tartools.csvfile
Class CSVFileWriter
java.lang.Object
org.evolvis.tartools.csvfile.CSVFile
org.evolvis.tartools.csvfile.CSVFileWriter
- Direct Known Subclasses:
SSVFileWriter
CSVFileWriter is a class derived from
CSVFile
used to format some fields into a new CSV file.
As of CSVFile 2.0, this will handle escaping fields whose contents contain the
field separator, text qualifier, carriage return and/or line feed properly.- Author:
- Fabrizio Fazzino
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final PrintWriterThe print writer linked to the CSV file to be written.protected StringSeparator between rows (default: LF)Fields inherited from class org.evolvis.tartools.csvfile.CSVFile
CR, CRLF, DEFAULT_FIELD_SEPARATOR, DEFAULT_TEXT_QUALIFIER, fieldSeparator, LF, textQualifier -
Constructor Summary
ConstructorsConstructorDescriptionCSVFileWriter(OutputStream stream) CSVFileWriter constructor with default values.CSVFileWriter(OutputStream stream, char sep, char qual) CSVFileWriter constructor with given field separator and text qualifier.CSVFileWriter(Writer writer) CSVFileWriter constructor with default values.CSVFileWriter(Writer writer, char sep, char qual) CSVFileWriter constructor with given field separator and text qualifier.CSVFileWriter(String outputFileName) CSVFileWriter constructor just needing the name of the CSV file that will be written.CSVFileWriter(String outputFileName, char sep) CSVFileWriter constructor with a given field separator.CSVFileWriter(String outputFileName, char sep, char qual) CSVFileWriter constructor with given field separator and text qualifier. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the output CSV file.Gets the current row separatorprotected StringprepareField(Object field) Prepares a field for output by stringifying the passed object and quoting it.voidsetRowSeparator(String separator) Sets the row separator to an arbitrary string.voidwriteFields(Iterable<?> fields) Joins the fields and writes them as a new line to the CSV file.Methods inherited from class org.evolvis.tartools.csvfile.CSVFile
getFieldSeparator, getTextQualifier, setFieldSeparator, setTextQualifier
-
Field Details
-
out
The print writer linked to the CSV file to be written. -
rowSeparator
Separator between rows (default: LF)
-
-
Constructor Details
-
CSVFileWriter
CSVFileWriter constructor just needing the name of the CSV file that will be written.- Parameters:
outputFileName- The name of the CSV file to be opened for writing- Throws:
IOException- if an error occurs while creating the file
-
CSVFileWriter
CSVFileWriter constructor with default values.- Parameters:
writer- TheWriterto be opened for writing
-
CSVFileWriter
CSVFileWriter constructor with default values. The CSV file will be written in UTF-8 encoding.- Parameters:
stream- TheOutputStreamto be opened for writing
-
CSVFileWriter
CSVFileWriter constructor with a given field separator.- Parameters:
outputFileName- The name of the CSV file to be opened for writingsep- The field separator to be used; overwrites the default one- Throws:
IOException- if an error occurs while creating the file
-
CSVFileWriter
CSVFileWriter constructor with given field separator and text qualifier.- Parameters:
outputFileName- The name of the CSV file to be opened for writingsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one- Throws:
IOException- if an error occurs while creating the file
-
CSVFileWriter
CSVFileWriter constructor with given field separator and text qualifier. The CSV file will be written in UTF-8 encoding.- Parameters:
stream- TheOutputStreamto be opened for writingsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one
-
CSVFileWriter
CSVFileWriter constructor with given field separator and text qualifier.- Parameters:
writer- TheWriterto be opened for writingsep- The field separator to be used; overwrites the default onequal- The text qualifier to be used; overwrites the default one
-
-
Method Details
-
getRowSeparator
Gets the current row separator- Returns:
- row separator (CR, LF or CRLF) as String
-
setRowSeparator
Sets the row separator to an arbitrary string. However, setting it to anything other than CR, LF, or a combination of these will be ignored by the field escape code, and is therefore undefined. Setting it toSystem.lineSeparator()might be sensible.- Parameters:
separator- to use (default: "\n")
-
close
public void close()Closes the output CSV file. -
writeFields
Joins the fields and writes them as a new line to the CSV file.- Parameters:
fields- The list of strings containing the fields
-
prepareField
Prepares a field for output by stringifying the passed object and quoting it.- Parameters:
field- to prepare- Returns:
- quoted string
-