Package csv.impl
Class CSVWriter
java.lang.Object
csv.impl.AbstractTableWriter
csv.impl.AbstractStreamTableWriter
csv.impl.CSVWriter
- All Implemented Interfaces:
TableWriter
public class CSVWriter extends AbstractStreamTableWriter
Implements functionality for writing CSV streams.
Example:
java.io.File f = new java.io.File("csv-test.csv");
CSVWriter out = new CSVWriter(f);
out.printRow(new Object[] { "0:0", "0:1", "0:2" });
out.printRow(new Object[] { "1:0", "1:1", "1:2" });
out.close();
- Author:
- RalphSchuster
-
Constructor Summary
Constructors Constructor Description CSVWriter()Default constructor.CSVWriter(java.io.File file)Constructor for writing into a file.CSVWriter(java.io.OutputStream out)Constructor for writing into a stream.CSVWriter(java.io.Writer out)Deprecated.UseCSVWriter(OutputStream)instead.CSVWriter(java.io.Writer out, boolean flush)Deprecated.CSVWriter(java.lang.String file)Constructor for writing into a file. -
Method Summary
Modifier and Type Method Description voidclose()Closes the underlying stream.protected booleancolumnNeedsDelimiting(java.lang.String s)Decides if a column value needs to be wrapped with delimiters.intgetColumnCount()Returns the columns written to the stream.java.lang.StringgetColumnDelimiter()Returns the column delimiter to be used.chargetColumnSeparator()Returns the column separator to be used.chargetCommentChar()Returns the character used for indicating commentsjava.lang.StringgetRowSeparator()Returns the row separator to be used.java.io.PrintWritergetWriter()Returns the underlying stream.protected voidinit()Initializes the writer.booleanisColumnDelimiterRequired()Returns whether column delimiters are always required.protected java.lang.StringprepareColumnValue(java.lang.String s)Replaces all occurrences of the delimiter by doubling it.java.lang.StringprepareComment(java.lang.String comment)Formats a comment for printingjava.lang.StringprepareRow(java.lang.Object[] columns)Formats a row for CSV output.voidprintComment(java.lang.String comment)Prints a comment into the CSV stream.voidprintComment(java.lang.String comment, int row, int column)Prints a comment into the stream.voidprintRow(java.lang.Object[] columns)Prints a new row into the CSV file.voidsetColumnDelimiter(java.lang.String s)Sets the column delimiter to be used.voidsetColumnDelimiterRequired(boolean b)Sets if column separators are always required or not.voidsetColumnSeparator(char s)Sets the column separator to be used.voidsetCommentChar(char commentChar)Sets the character to be used for indicating commentsvoidsetRowSeparator(java.lang.String s)Sets the row separator to be used.Methods inherited from class csv.impl.AbstractStreamTableWriter
createOutputStreamWriter, createWriter, getCharsetEncoder, getOutputStream, setCharset, setCharset, setCharsetEncoder, setOutputStreamMethods inherited from class csv.impl.AbstractTableWriter
convert, convert, getRowCount, getTypeConversionHandler, incrementRowCount, printRow, printRow, printRow, registerTypeConversionHandler, unregisterTypeConversionHandler
-
Constructor Details
-
CSVWriter
public CSVWriter()Default constructor. -
CSVWriter
public CSVWriter(java.io.File file) throws java.io.IOExceptionConstructor for writing into a file.- Parameters:
file- file- Throws:
java.io.IOException- when an exception occurs
-
CSVWriter
public CSVWriter(java.io.OutputStream out)Constructor for writing into a stream.- Parameters:
out- output stream
-
CSVWriter
@Deprecated public CSVWriter(java.io.Writer out)Deprecated.UseCSVWriter(OutputStream)instead.For backwards compatibility only.- Parameters:
out- out writer
-
CSVWriter
@Deprecated public CSVWriter(java.io.Writer out, boolean flush)Deprecated.For backwards compatibility only.- Parameters:
out- out writerflush- has no influence and is for compatibility reasons only
-
CSVWriter
public CSVWriter(java.lang.String file) throws java.io.IOExceptionConstructor for writing into a file.- Parameters:
file- file- Throws:
java.io.IOException- when an exception occurs
-
-
Method Details
-
init
protected void init()Initializes the writer.- Overrides:
initin classAbstractTableWriter- See Also:
AbstractTableWriter.init()
-
getWriter
public java.io.PrintWriter getWriter()Returns the underlying stream.- Overrides:
getWriterin classAbstractStreamTableWriter- Returns:
- the writer object
- See Also:
AbstractStreamTableWriter.getWriter()
-
close
public void close()Closes the underlying stream.- Specified by:
closein interfaceTableWriter- Overrides:
closein classAbstractStreamTableWriter- See Also:
AbstractStreamTableWriter.close()
-
setColumnDelimiter
public void setColumnDelimiter(java.lang.String s)Sets the column delimiter to be used. Default are double-quotes. The usage of the delimiters is defined byisColumnDelimiterRequired().- Parameters:
s- the new delimiter
-
getColumnDelimiter
public java.lang.String getColumnDelimiter()Returns the column delimiter to be used. Default are double-quotes.- Returns:
- the column delimiter being used.
-
setColumnSeparator
public void setColumnSeparator(char s)Sets the column separator to be used. Default is semi-colon.- Parameters:
s- new separator character
-
getColumnSeparator
public char getColumnSeparator()Returns the column separator to be used. Default is semi-colon.- Returns:
- the column separator character being used
-
setRowSeparator
public void setRowSeparator(java.lang.String s)Sets the row separator to be used. Actually you should never change this character as it will break the definition of a CSV stream. Default is newline character.- Parameters:
s- new separator
-
getRowSeparator
public java.lang.String getRowSeparator()Returns the row separator to be used. Default is a newline character.- Returns:
- the row separator character.
-
setColumnDelimiterRequired
public void setColumnDelimiterRequired(boolean b)Sets if column separators are always required or not. Usually, column delimiters are written only when the column value contains special characters, such as the delimiters for columns and rows. Default value is false.- Parameters:
b- true when delimiters shall always be written.
-
isColumnDelimiterRequired
public boolean isColumnDelimiterRequired()Returns whether column delimiters are always required. Default value is false.- Returns:
- true if column delimiters are written on each column.
-
getCommentChar
public char getCommentChar()Returns the character used for indicating comments- Returns:
- the commentChar
-
setCommentChar
public void setCommentChar(char commentChar)Sets the character to be used for indicating comments- Parameters:
commentChar- the commentChar to set
-
prepareRow
public java.lang.String prepareRow(java.lang.Object[] columns)Formats a row for CSV output.- Parameters:
columns- columns to be prepared- Returns:
- string ready to be printed in CSV
-
printRow
public void printRow(java.lang.Object[] columns) throws java.io.IOExceptionPrints a new row into the CSV file. This is the method where an actual CSV row will be printed. The columns are prepared to follow the CSV syntax rules and definitions. The underlying stream in flushed immediately.- Parameters:
columns- array of column values.- Throws:
java.io.IOException- when an exception occurs
-
prepareComment
public java.lang.String prepareComment(java.lang.String comment)Formats a comment for printing- Parameters:
comment- comment to be printed- Returns:
- string ready to be written to CSV
-
printComment
public void printComment(java.lang.String comment) throws java.io.IOExceptionPrints a comment into the CSV stream.- Specified by:
printCommentin interfaceTableWriter- Overrides:
printCommentin classAbstractTableWriter- Parameters:
comment- comment to write- Throws:
java.io.IOException- when an exception occurs
-
printComment
public void printComment(java.lang.String comment, int row, int column) throws java.io.IOExceptionPrints a comment into the stream. Note that not all implementations support comments.- Specified by:
printCommentin interfaceTableWriter- Overrides:
printCommentin classAbstractTableWriter- Parameters:
comment- comment to writerow- index of row for commentcolumn- index of column for comment- Throws:
java.io.IOException- when an exception occurs
-
columnNeedsDelimiting
protected boolean columnNeedsDelimiting(java.lang.String s)Decides if a column value needs to be wrapped with delimiters.- Parameters:
s- column value to check- Returns:
- true if value must be wrapped in output
-
prepareColumnValue
protected java.lang.String prepareColumnValue(java.lang.String s)Replaces all occurrences of the delimiter by doubling it.- Parameters:
s- column value to parse- Returns:
- value with replaced delimiters.
-
getColumnCount
public int getColumnCount()Returns the columns written to the stream.- Returns:
- the columnCount
-