Package de.hybris.platform.util
Class CSVWriter
- java.lang.Object
-
- de.hybris.platform.util.CSVWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
- Direct Known Subclasses:
SyncScheduleWriter
public class CSVWriter extends java.lang.Object implements java.io.CloseableThis 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
Constructors Constructor Description CSVWriter(java.io.File file, java.lang.String encoding)Creates a new CSVWriter with the given File and encoding.CSVWriter(java.io.File file, java.lang.String encoding, boolean append)Creates a new CSVWriter with the given File and encoding.CSVWriter(java.io.OutputStream os, java.lang.String encoding)Default Constructor.CSVWriter(java.io.Writer writer)Creates a new CSVWriter with the given writer.CSVWriter(java.lang.String fileName, java.lang.String encoding)Creates a new CSVWriter with the given file name and encoding.CSVWriter(java.lang.String fileName, java.lang.String encoding, boolean append)Creates a new CSVWriter with the given file name and encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the writer.voidcloseQuietly()Close the writer quietly.java.lang.StringcreateCSVLine(java.util.Map fields)Creates a valid CSV-data line from given field map.chargetCommentchar()Gets used char for commenting a line.protected chargetDefaultCommentChar()protected chargetDefaultFieldSeparator()protected java.lang.StringgetDefaultLineBreak()protected java.lang.String[]getDefaultLineSeparators()protected chargetDefaultTextSeparator()chargetFieldseparator()Gets used char for separating fields.java.lang.StringgetLinebreak()Gets used text for separating lines.chargetTextseparator()Gets used char for enclosing fields if they contain field separators.java.io.WritergetWriter()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(java.lang.String linebreak)Sets the text for separating lines.voidsetTextseparator(char textseparator)Sets the character for enclosing a field.intwrite(java.util.List data)Writes the given List to the writer.voidwrite(java.util.Map linedata)Writes a single line to the writer.voidwriteComment(java.lang.String scrline)Writes the given line as a comment line to the writer/file.voidwriteSrcLine(java.lang.String scrline)The given line will be written to the writer without modification to the line.
-
-
-
Constructor Detail
-
CSVWriter
public CSVWriter(java.io.File file, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionCreates a new CSVWriter with the given File and encoding. Default encoding is- Parameters:
file- the fileencoding- the encoding- Throws:
java.io.UnsupportedEncodingException- if false encodingjava.io.FileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(java.io.File file, java.lang.String encoding, boolean append) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionCreates 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:
java.io.UnsupportedEncodingException- if false encodingjava.io.FileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(java.io.OutputStream os, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionDefault Constructor. Creates a new CSVWriter with the given output stream and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
os- the outputstreamencoding- the encoding- Throws:
java.io.UnsupportedEncodingException- if false encoding
-
CSVWriter
public CSVWriter(java.lang.String fileName, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionCreates a new CSVWriter with the given file name and encoding. Default encoding isCSVConstants.DEFAULT_ENCODING.- Parameters:
fileName- the file nameencoding- the encoding- Throws:
java.io.UnsupportedEncodingException- if false encodingjava.io.FileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(java.lang.String fileName, java.lang.String encoding, boolean append) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundExceptionCreates 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:
java.io.UnsupportedEncodingException- if false encodingjava.io.FileNotFoundException- if file not found
-
CSVWriter
public CSVWriter(java.io.Writer writer)
Creates a new CSVWriter with the given writer.- Parameters:
writer- the writer
-
-
Method Detail
-
getDefaultCommentChar
protected char getDefaultCommentChar()
-
getDefaultFieldSeparator
protected char getDefaultFieldSeparator()
-
getDefaultTextSeparator
protected char getDefaultTextSeparator()
-
getDefaultLineBreak
protected java.lang.String getDefaultLineBreak()
-
getDefaultLineSeparators
protected java.lang.String[] getDefaultLineSeparators()
-
close
public void close() throws java.io.IOExceptionCloses the writer.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.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
public java.lang.String createCSVLine(java.util.Map fields)
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
public java.lang.String getLinebreak()
Gets used text for separating lines.- Returns:
- used text for separating lines
-
getWriter
public java.io.Writer 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
public void setLinebreak(java.lang.String linebreak)
Sets the text for separating lines.- Parameters:
linebreak- text for separating lines
-
write
public int write(java.util.List data) throws java.io.IOExceptionWrites the given List to the writer. This method callswrite(Map)- Parameters:
data- a List which contains Maps- Returns:
- how many lines are written
- Throws:
java.io.IOException- error while writing
-
write
public void write(java.util.Map linedata) throws java.io.IOExceptionWrites 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:
java.io.IOException- error while writing
-
writeComment
public void writeComment(java.lang.String scrline) throws java.io.IOExceptionWrites the given line as a comment line to the writer/file. Set the comment char withsetCommentchar(char).- Parameters:
scrline- the comment line- Throws:
java.io.IOException- error while writing
-
writeSrcLine
public void writeSrcLine(java.lang.String scrline) throws java.io.IOExceptionThe 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:
java.io.IOException- error while writing
-
-