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. Note: the presence of a sole quote character (“text qualifier”) in an output field breaks the CSV spec as the field is then not quoted‽- Author:
- Fabrizio Fazzino
-
-
Field Summary
Fields Modifier and Type Field Description protected PrintWriteroutThe print writer linked to the CSV file to be written.-
Fields inherited from class org.evolvis.tartools.csvfile.CSVFile
DEFAULT_FIELD_SEPARATOR, DEFAULT_TEXT_QUALIFIER, fieldSeparator, textQualifier
-
-
Constructor Summary
Constructors Constructor Description CSVFileWriter(Writer writer, char sep, char qual)CSVFileWriter constructor with given field separator and text qualifier.CSVFileWriter(String outputFileName)CSVFileWriter constructor just need 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.protected voidemitRowSeparator()Called after a list of fields has been output.protected StringprepareField(Object field)Prepares a field for output by stringifying the passed object and quoting it.voidwriteFields(List<?> 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.
-
-
Constructor Detail
-
CSVFileWriter
public CSVFileWriter(String outputFileName) throws IOException
CSVFileWriter constructor just need 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(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(Writer writer, char sep, char qual)
CSVFileWriter constructor with given field separator and text qualifier.- Parameters:
writer- The Writer 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
-
-
Method Detail
-
close
public void close()
Closes the output CSV file.
-
writeFields
public void writeFields(List<?> fields)
Joins the fields and writes them as a new line to the CSV file.- Parameters:
fields- The list of strings containing the fields
-
emitRowSeparator
protected void emitRowSeparator()
Called after a list of fields has been output.
-
-