Package de.hybris.platform.util
Class CSVWriter
java.lang.Object
de.hybris.platform.util.CSVWriter
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
SyncScheduleWriter
This class writes maps of text fields to a CSV OutputStream. Each map of text fields (a field is mapped to the
desired position in line) represents a line which will be transformed to a valid CSV line and written to the stream.
The format of the CSV-output is conform to RFC-4180 if using
default settings.
Use of this CSVWriter:
- Create the Writer
csvwriter = new CSVWriter(outputStream, null ); // null for default encoding
Use the constructor to set the encoding and the stream. There are also other constructors available. - Configure the Writer
Example:csvwriter.setTextseparator('?');
If you skip this step, following defaults are set:
commentOut = '#'//chars for comment linesfieldseparator = ';'//separate the CSV fieldstextseparator = '\"'//enclose a CSV textlinebreak = '\n'//separates lines
- Use the Writer
Map line = new HashMap(); line.add(new Integer(0), "firstField"); line.add(new Integer(1), "secondField"); csvwriter.write(line); csvwriter.writeComment("a comment line"); - Close the Writer
Do not forget to callcsvwriter.close()to close the writer. Without you can not access the destination file.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new CSVWriter with the given File and encoding.Creates a new CSVWriter with the given File and encoding.CSVWriter(OutputStream os, String encoding) Default Constructor.Creates a new CSVWriter with the given writer.Creates a new CSVWriter with the given file name and encoding.Creates a new CSVWriter with the given file name and encoding. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the writer.voidClose the writer quietly.createCSVLine(Map fields) Creates a valid CSV-data line from given field map.charGets used char for commenting a line.protected charprotected charprotected Stringprotected String[]protected charcharGets used char for separating fields.Gets used text for separating lines.charGets used char for enclosing fields if they contain field separators.Gets the underlying writer instance.voidsetCommentchar(char commentchar) Sets the character for commenting a line.voidsetFieldseparator(char fieldseparator) Sets the character for separating fields.voidsetLinebreak(String linebreak) Sets the text for separating lines.voidsetTextseparator(char textseparator) Sets the character for enclosing a field.intWrites the given List to the writer.voidWrites a single line to the writer.voidwriteComment(String scrline) Writes the given line as a comment line to the writer/file.voidwriteSrcLine(String scrline) The given line will be written to the writer without modification to the line.
-
Constructor Details
-
CSVWriter
public CSVWriter(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException Creates a new CSVWriter with the given File and encoding. Default encoding is- Parameters:
file- the fileencoding- the encoding- Throws:
UnsupportedEncodingException- if false encodingFileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(File file, String encoding, boolean append) throws UnsupportedEncodingException, FileNotFoundException Creates a new CSVWriter with the given File and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
file- the fileencoding- the encodingappend- write to file in append mode (written data will be added to the existing one)?- Throws:
UnsupportedEncodingException- if false encodingFileNotFoundException- if file not found
-
CSVWriter
Default Constructor. Creates a new CSVWriter with the given output stream and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
os- the outputstreamencoding- the encoding- Throws:
UnsupportedEncodingException- if false encoding
-
CSVWriter
public CSVWriter(String fileName, String encoding) throws UnsupportedEncodingException, FileNotFoundException Creates a new CSVWriter with the given file name and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
fileName- the file nameencoding- the encoding- Throws:
UnsupportedEncodingException- if false encodingFileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(String fileName, String encoding, boolean append) throws UnsupportedEncodingException, FileNotFoundException Creates a new CSVWriter with the given file name and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
fileName- the file nameencoding- the encodingappend- write to file in append mode (written data will be added to the existing one)?- Throws:
UnsupportedEncodingException- if false encodingFileNotFoundException- if file not found
-
CSVWriter
Creates a new CSVWriter with the given writer.- Parameters:
writer- the writer
-
-
Method Details
-
getDefaultCommentChar
protected char getDefaultCommentChar() -
getDefaultFieldSeparator
protected char getDefaultFieldSeparator() -
getDefaultTextSeparator
protected char getDefaultTextSeparator() -
getDefaultLineBreak
-
getDefaultLineSeparators
-
close
Closes the writer.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- error while closing
-
closeQuietly
public void closeQuietly()Close the writer quietly. The IOException will be catched and if the debug mode is enabled the exeption message is written to the log. -
createCSVLine
Creates a valid CSV-data line from given field map.- Parameters:
fields- map with field texts mapped to the desired field position- Returns:
- created CSV-line
-
getCommentchar
public char getCommentchar()Gets used char for commenting a line.- Returns:
- used char for commenting a line
-
getFieldseparator
public char getFieldseparator()Gets used char for separating fields.- Returns:
- used char for separating fields
-
getTextseparator
public char getTextseparator()Gets used char for enclosing fields if they contain field separators.- Returns:
- used char for enclosing fields if they contain field separators
-
getLinebreak
Gets used text for separating lines.- Returns:
- used text for separating lines
-
getWriter
Gets the underlying writer instance.- Returns:
- the underlying writer instance
-
setCommentchar
public void setCommentchar(char commentchar) Sets the character for commenting a line.- Parameters:
commentchar- character for commenting
-
setFieldseparator
public void setFieldseparator(char fieldseparator) Sets the character for separating fields.- Parameters:
fieldseparator- character for separating fields
-
setTextseparator
public void setTextseparator(char textseparator) Sets the character for enclosing a field.- Parameters:
textseparator- character for enclosing field
-
setLinebreak
Sets the text for separating lines.- Parameters:
linebreak- text for separating lines
-
write
Writes the given List to the writer. This method callswrite(Map)- Parameters:
data- a List which contains Maps- Returns:
- how many lines are written
- Throws:
IOException- error while writing
-
write
Writes a single line to the writer. SeeCSVReader.parseLine(String)how the map looks like. The line will be quoted in the textseparator char.
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n:Integer, Field_n:String }
}
- Parameters:
linedata- the line which will be written- Throws:
IOException- error while writing
-
writeComment
Writes the given line as a comment line to the writer/file. Set the comment char withsetCommentchar(char).- Parameters:
scrline- the comment line- Throws:
IOException- error while writing
-
writeSrcLine
The given line will be written to the writer without modification to the line. At the end a '\n' is always written.- Parameters:
scrline- the line- Throws:
IOException- error while writing
-