Package org.evolvis.tartools.csvfile
Class CSVFileWriter
- java.lang.Object
-
- org.evolvis.tartools.csvfile.CSVFile
-
- org.evolvis.tartools.csvfile.CSVFileWriter
-
- Direct Known Subclasses:
SSVFileWriter
public class CSVFileWriter extends CSVFile
CSVFileWriter is a class derived fromCSVFileused 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
Fields Modifier and Type Field Description protected PrintWriteroutThe print writer linked to the CSV file to be written.protected StringrowSeparatorSeparator 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
Constructors Constructor Description CSVFileWriter(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the output CSV file.StringgetRowSeparator()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 Detail
-
out
protected final PrintWriter out
The print writer linked to the CSV file to be written.
-
rowSeparator
protected String rowSeparator
Separator between rows (default: LF)
-
-
Constructor Detail
-
CSVFileWriter
public CSVFileWriter(String outputFileName) throws IOException
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
public CSVFileWriter(Writer writer)
CSVFileWriter constructor with default values.- Parameters:
writer- TheWriterto be opened for writing
-
CSVFileWriter
public CSVFileWriter(OutputStream stream)
CSVFileWriter constructor with default values. The CSV file will be written in UTF-8 encoding.- Parameters:
stream- TheOutputStreamto be opened for writing
-
CSVFileWriter
public CSVFileWriter(String outputFileName, char sep) throws IOException
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
public CSVFileWriter(String outputFileName, char sep, char qual) throws IOException
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
public CSVFileWriter(OutputStream stream, char sep, char qual)
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
public CSVFileWriter(Writer writer, char sep, char qual)
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 Detail
-
getRowSeparator
public String getRowSeparator()
Gets the current row separator- Returns:
- row separator (CR, LF or CRLF) as String
-
setRowSeparator
public void setRowSeparator(String separator)
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
public void writeFields(Iterable<?> fields)
Joins the fields and writes them as a new line to the CSV file.- Parameters:
fields- The list of strings containing the fields
-
-