Package csv.impl
Class CSVWriter
java.lang.Object
csv.impl.AbstractTableWriter
csv.impl.AbstractStreamTableWriter
csv.impl.CSVWriter
- All Implemented Interfaces:
TableWriter
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
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the underlying stream.protected boolean
Decides if a column value needs to be wrapped with delimiters.int
Returns the columns written to the stream.Returns the column delimiter to be used.char
Returns the column separator to be used.char
Returns the character used for indicating commentsReturns the row separator to be used.Returns the underlying stream.protected void
init()
Initializes the writer.boolean
Returns whether column delimiters are always required.protected String
Replaces all occurrences of the delimiter by doubling it.prepareComment
(String comment) Formats a comment for printingprepareRow
(Object[] columns) Formats a row for CSV output.void
printComment
(String comment) Prints a comment into the CSV stream.void
printComment
(String comment, int row, int column) Prints a comment into the stream.void
Prints a new row into the CSV file.void
Sets the column delimiter to be used.void
setColumnDelimiterRequired
(boolean b) Sets if column separators are always required or not.void
setColumnSeparator
(char s) Sets the column separator to be used.void
setCommentChar
(char commentChar) Sets the character to be used for indicating commentsvoid
Sets the row separator to be used.Methods inherited from class csv.impl.AbstractStreamTableWriter
createOutputStreamWriter, createWriter, getCharsetEncoder, getOutputStream, setCharset, setCharset, setCharsetEncoder, setOutputStream
Methods inherited from class csv.impl.AbstractTableWriter
convert, getMapper, getRowCount, incrementRowCount, printRow, printRow, printRow, setMapper
-
Constructor Details
-
CSVWriter
public CSVWriter()Default constructor. -
CSVWriter
Constructor for writing into a file.- Parameters:
file
- file- Throws:
IOException
- when an exception occurs
-
CSVWriter
Constructor for writing into a stream.- Parameters:
out
- output stream
-
CSVWriter
Constructor for writing into a file.- Parameters:
file
- file- Throws:
IOException
- when an exception occurs
-
-
Method Details
-
init
protected void init()Initializes the writer.- Overrides:
init
in classAbstractTableWriter
- See Also:
-
getWriter
Returns the underlying stream.- Overrides:
getWriter
in classAbstractStreamTableWriter
- Returns:
- the writer object
- See Also:
-
close
public void close()Closes the underlying stream.- Specified by:
close
in interfaceTableWriter
- Overrides:
close
in classAbstractStreamTableWriter
- See Also:
-
setColumnDelimiter
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
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
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
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
Formats a row for CSV output.- Parameters:
columns
- columns to be prepared- Returns:
- string ready to be printed in CSV
-
printRow
Prints 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:
IOException
- when an exception occurs
-
prepareComment
Formats a comment for printing- Parameters:
comment
- comment to be printed- Returns:
- string ready to be written to CSV
-
printComment
Prints a comment into the CSV stream.- Specified by:
printComment
in interfaceTableWriter
- Overrides:
printComment
in classAbstractTableWriter
- Parameters:
comment
- comment to write- Throws:
IOException
- when an exception occurs
-
printComment
Prints a comment into the stream. Note that not all implementations support comments.- Specified by:
printComment
in interfaceTableWriter
- Overrides:
printComment
in classAbstractTableWriter
- Parameters:
comment
- comment to writerow
- index of row for commentcolumn
- index of column for comment- Throws:
IOException
- when an exception occurs
-
columnNeedsDelimiting
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
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
-