public class CSVWriter
extends java.lang.Object
implements java.io.Closeable
Use of this CSVWriter:
csvwriter = new CSVWriter(outputStream, null ); // null for default encodingcsvwriter.setTextseparator('?');commentOut = '#' //chars for comment linesfieldseparator = ';' //separate the CSV fieldstextseparator = '\"' //enclose a CSV textlinebreak = '\n' //separates lines
Map line = new HashMap();
line.add(new Integer(0), "firstField");
line.add(new Integer(1), "secondField");
csvwriter.write(line);
csvwriter.writeComment("a comment line");
csvwriter.close() to close the writer. Without you can not access the destination
file.| Constructor and 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.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.
|
CSVWriter(java.io.Writer writer)
Creates a new CSVWriter with the given writer.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close()
Closes the writer.
|
void |
closeQuietly()
Close the writer quietly.
|
java.lang.String |
createCSVLine(java.util.Map fields)
Creates a valid CSV-data line from given field map.
|
char |
getCommentchar()
Gets used char for commenting a line.
|
protected char |
getDefaultCommentChar() |
protected char |
getDefaultFieldSeparator() |
protected java.lang.String |
getDefaultLineBreak() |
protected java.lang.String[] |
getDefaultLineSeparators() |
protected char |
getDefaultTextSeparator() |
char |
getFieldseparator()
Gets used char for separating fields.
|
java.lang.String |
getLinebreak()
Gets used text for separating lines.
|
char |
getTextseparator()
Gets used char for enclosing fields if they contain field separators.
|
java.io.Writer |
getWriter()
Gets the underlying writer instance.
|
void |
setCommentchar(char commentchar)
Sets the character for commenting a line.
|
void |
setFieldseparator(char fieldseparator)
Sets the character for separating fields.
|
void |
setLinebreak(java.lang.String linebreak)
Sets the text for separating lines.
|
void |
setTextseparator(char textseparator)
Sets the character for enclosing a field.
|
int |
write(java.util.List data)
Writes the given List to the writer.
|
void |
write(java.util.Map linedata)
Writes a single line to the writer.
|
void |
writeComment(java.lang.String scrline)
Writes the given line as a comment line to the writer/file.
|
void |
writeSrcLine(java.lang.String scrline)
The given line will be written to the writer without modification to the line.
|
public CSVWriter(java.io.File file,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
file - the fileencoding - the encodingjava.io.UnsupportedEncodingException - if false encodingjava.io.FileNotFoundException - if file not foundpublic CSVWriter(java.io.File file,
java.lang.String encoding,
boolean append)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
CSVConstants.DEFAULT_ENCODING.file - the fileencoding - the encodingappend - write to file in append mode (written data will be added to the existing one)?java.io.UnsupportedEncodingException - if false encodingjava.io.FileNotFoundException - if file not foundpublic CSVWriter(java.io.OutputStream os,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException
CSVConstants.DEFAULT_ENCODING.os - the outputstreamencoding - the encodingjava.io.UnsupportedEncodingException - if false encodingpublic CSVWriter(java.lang.String fileName,
java.lang.String encoding)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
CSVConstants.DEFAULT_ENCODING.fileName - the file nameencoding - the encodingjava.io.UnsupportedEncodingException - if false encodingjava.io.FileNotFoundException - if file not foundpublic CSVWriter(java.lang.String fileName,
java.lang.String encoding,
boolean append)
throws java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
CSVConstants.DEFAULT_ENCODING.fileName - the file nameencoding - the encodingappend - write to file in append mode (written data will be added to the existing one)?java.io.UnsupportedEncodingException - if false encodingjava.io.FileNotFoundException - if file not foundpublic CSVWriter(java.io.Writer writer)
writer - the writerprotected char getDefaultCommentChar()
protected char getDefaultFieldSeparator()
protected char getDefaultTextSeparator()
protected java.lang.String getDefaultLineBreak()
protected java.lang.String[] getDefaultLineSeparators()
public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOException - error while closingpublic void closeQuietly()
public java.lang.String createCSVLine(java.util.Map fields)
fields - map with field texts mapped to the desired field positionpublic char getCommentchar()
public char getFieldseparator()
public char getTextseparator()
public java.lang.String getLinebreak()
public java.io.Writer getWriter()
public void setCommentchar(char commentchar)
commentchar - character for commentingpublic void setFieldseparator(char fieldseparator)
fieldseparator - character for separating fieldspublic void setTextseparator(char textseparator)
textseparator - character for enclosing fieldpublic void setLinebreak(java.lang.String linebreak)
linebreak - text for separating linespublic int write(java.util.List data)
throws java.io.IOException
write(Map)data - a List which contains Mapsjava.io.IOException - error while writingpublic void write(java.util.Map linedata)
throws java.io.IOException
CSVReader.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 }
}
linedata - the line which will be writtenjava.io.IOException - error while writingpublic void writeComment(java.lang.String scrline)
throws java.io.IOException
setCommentchar(char).scrline - the comment linejava.io.IOException - error while writingpublic void writeSrcLine(java.lang.String scrline)
throws java.io.IOException
scrline - the linejava.io.IOException - error while writingCopyright © 2018 SAP SE. All Rights Reserved.