Package com.csvreader
Class CsvWriter
- java.lang.Object
-
- com.csvreader.CsvWriter
-
public class CsvWriter extends java.lang.ObjectA stream based writer for writing delimited text data to a file or a stream.
-
-
Field Summary
Fields Modifier and Type Field Description static intESCAPE_MODE_BACKSLASHUse a backslash character before the text qualifier to represent an occurrence of the text qualifier.static intESCAPE_MODE_DOUBLEDDouble up the text qualifier to represent an occurrence of the text qualifier.
-
Constructor Summary
Constructors Constructor Description CsvWriter(java.io.OutputStream outputStream, char delimiter, java.nio.charset.Charset charset)Creates aCsvWriterobject using an OutputStream to write data to.CsvWriter(java.io.Writer outputStream, char delimiter)Creates aCsvWriterobject using a Writer to write data to.CsvWriter(java.lang.String fileName)Creates aCsvWriterobject using a file as the data destination. Uses a comma as the column delimiter and ISO-8859-1 as theCharset.CsvWriter(java.lang.String fileName, char delimiter, java.nio.charset.Charset charset)Creates aCsvWriterobject using a file as the data destination.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes and releases all related resources.voidendRecord()Ends the current record by sending the record delimiter.protected voidfinalize()voidflush()Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.chargetComment()chargetDelimiter()Gets the character being used as the column delimiter.intgetEscapeMode()booleangetForceQualifier()Whether fields will be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.chargetRecordDelimiter()chargetTextQualifier()Gets the character to use as a text qualifier in the data.booleangetUseTextQualifier()Whether text qualifiers will be used while writing data or not.static java.lang.Stringreplace(java.lang.String original, java.lang.String pattern, java.lang.String replace)voidsetComment(char comment)voidsetDelimiter(char delimiter)Sets the character to use as the column delimiter.voidsetEscapeMode(int escapeMode)voidsetForceQualifier(boolean forceQualifier)Use this to force all fields to be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.voidsetRecordDelimiter(char recordDelimiter)Sets the character to use as the record delimiter.voidsetTextQualifier(char textQualifier)Sets the character to use as a text qualifier in the data.voidsetUseTextQualifier(boolean useTextQualifier)Sets whether text qualifiers will be used while writing data or not.voidwrite(java.lang.String content)Writes another column of data to this record. Does not preserve leading and trailing whitespace in this column of data.voidwrite(java.lang.String content, boolean preserveSpaces)Writes another column of data to this record.voidwriteComment(java.lang.String commentText)voidwriteRecord(java.lang.String[] values)Writes a new record using the passed in array of values.voidwriteRecord(java.lang.String[] values, boolean preserveSpaces)Writes a new record using the passed in array of values.
-
-
-
Field Detail
-
ESCAPE_MODE_DOUBLED
public static final int ESCAPE_MODE_DOUBLED
Double up the text qualifier to represent an occurrence of the text qualifier.- See Also:
- Constant Field Values
-
ESCAPE_MODE_BACKSLASH
public static final int ESCAPE_MODE_BACKSLASH
Use a backslash character before the text qualifier to represent an occurrence of the text qualifier.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CsvWriter
public CsvWriter(java.lang.String fileName, char delimiter, java.nio.charset.Charset charset)Creates aCsvWriterobject using a file as the data destination.- Parameters:
fileName- The path to the file to output the data.delimiter- The character to use as the column delimiter.charset- TheCharsetto use while writing the data.
-
CsvWriter
public CsvWriter(java.lang.String fileName)
Creates aCsvWriterobject using a file as the data destination. Uses a comma as the column delimiter and ISO-8859-1 as theCharset.- Parameters:
fileName- The path to the file to output the data.
-
CsvWriter
public CsvWriter(java.io.Writer outputStream, char delimiter)Creates aCsvWriterobject using a Writer to write data to.- Parameters:
outputStream- The stream to write the column delimited data to.delimiter- The character to use as the column delimiter.
-
CsvWriter
public CsvWriter(java.io.OutputStream outputStream, char delimiter, java.nio.charset.Charset charset)Creates aCsvWriterobject using an OutputStream to write data to.- Parameters:
outputStream- The stream to write the column delimited data to.delimiter- The character to use as the column delimiter.charset- TheCharsetto use while writing the data.
-
-
Method Detail
-
getDelimiter
public char getDelimiter()
Gets the character being used as the column delimiter.- Returns:
- The character being used as the column delimiter.
-
setDelimiter
public void setDelimiter(char delimiter)
Sets the character to use as the column delimiter.- Parameters:
delimiter- The character to use as the column delimiter.
-
getRecordDelimiter
public char getRecordDelimiter()
-
setRecordDelimiter
public void setRecordDelimiter(char recordDelimiter)
Sets the character to use as the record delimiter.- Parameters:
recordDelimiter- The character to use as the record delimiter. Default is combination of standard end of line characters for Windows, Unix, or Mac.
-
getTextQualifier
public char getTextQualifier()
Gets the character to use as a text qualifier in the data.- Returns:
- The character to use as a text qualifier in the data.
-
setTextQualifier
public void setTextQualifier(char textQualifier)
Sets the character to use as a text qualifier in the data.- Parameters:
textQualifier- The character to use as a text qualifier in the data.
-
getUseTextQualifier
public boolean getUseTextQualifier()
Whether text qualifiers will be used while writing data or not.- Returns:
- Whether text qualifiers will be used while writing data or not.
-
setUseTextQualifier
public void setUseTextQualifier(boolean useTextQualifier)
Sets whether text qualifiers will be used while writing data or not.- Parameters:
useTextQualifier- Whether to use a text qualifier while writing data or not.
-
getEscapeMode
public int getEscapeMode()
-
setEscapeMode
public void setEscapeMode(int escapeMode)
-
setComment
public void setComment(char comment)
-
getComment
public char getComment()
-
getForceQualifier
public boolean getForceQualifier()
Whether fields will be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.- Returns:
- Whether fields will be forced to be qualified or not.
-
setForceQualifier
public void setForceQualifier(boolean forceQualifier)
Use this to force all fields to be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field. Default is false.- Parameters:
forceQualifier- Whether to force the fields to be qualified or not.
-
write
public void write(java.lang.String content, boolean preserveSpaces) throws java.io.IOExceptionWrites another column of data to this record.- Parameters:
content- The data for the new column.preserveSpaces- Whether to preserve leading and trailing whitespace in this column of data.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
write
public void write(java.lang.String content) throws java.io.IOExceptionWrites another column of data to this record. Does not preserve leading and trailing whitespace in this column of data.- Parameters:
content- The data for the new column.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
writeComment
public void writeComment(java.lang.String commentText) throws java.io.IOException- Throws:
java.io.IOException
-
writeRecord
public void writeRecord(java.lang.String[] values, boolean preserveSpaces) throws java.io.IOExceptionWrites a new record using the passed in array of values.- Parameters:
values- Values to be written.preserveSpaces- Whether to preserver leading and trailing spaces in columns while writing out to the record or not.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
writeRecord
public void writeRecord(java.lang.String[] values) throws java.io.IOExceptionWrites a new record using the passed in array of values.- Parameters:
values- Values to be written.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
endRecord
public void endRecord() throws java.io.IOExceptionEnds the current record by sending the record delimiter.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
flush
public void flush() throws java.io.IOExceptionClears all buffers for the current writer and causes any buffered data to be written to the underlying device.- Throws:
java.io.IOException- Thrown if an error occurs while writing data to the destination stream.
-
close
public void close()
Closes and releases all related resources.
-
finalize
protected void finalize()
- Overrides:
finalizein classjava.lang.Object
-
replace
public static java.lang.String replace(java.lang.String original, java.lang.String pattern, java.lang.String replace)
-
-