Package org.supercsv.io
Class AbstractCsvWriter
- java.lang.Object
-
- org.supercsv.io.AbstractCsvWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,ICsvWriter
- Direct Known Subclasses:
CsvBeanWriter,CsvListWriter,CsvMapWriter,CsvResultSetWriter
public abstract class AbstractCsvWriter extends java.lang.Object implements ICsvWriter
Defines the standard behaviour of a CSV writer.
-
-
Field Summary
Fields Modifier and Type Field Description private intcolumnNumberprivate CsvEncoderencoderprivate intlineNumberprivate CsvPreferencepreferenceprivate introwNumberprivate java.io.Writerwriter
-
Constructor Summary
Constructors Constructor Description AbstractCsvWriter(java.io.Writer writer, CsvPreference preference)Constructs a new AbstractCsvWriter with the supplied writer and preferences.AbstractCsvWriter(java.io.Writer writer, CsvPreference preference, boolean bufferizeWriter)Constructs a new AbstractCsvWriter with the supplied writer, preferences and option to wrap the writer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the underlying writer, flushing it first.voidflush()Flushes the underlying writer.intgetLineNumber()Gets the current position in the file.intgetRowNumber()Gets the current row number (i.e.protected voidincrementRowAndLineNo()In order to maintain the current row and line numbers, this method must be called at the very beginning of every write method implemented in concrete CSV writers.voidwriteComment(java.lang.String comment)Writes a single-line comment to the CSV file (the comment must already include any special comment characters e.g.voidwriteHeader(java.lang.String... header)Writes the header of the CSV file.protected voidwriteRow(java.lang.Object... columns)Writes one or more Object columns as a line to the CsvWriter.protected voidwriteRow(java.lang.String... columns)Writes one or more String columns as a line to the CsvWriter.protected voidwriteRow(java.util.List<?> columns)Writes a List of columns as a line to the CsvWriter.
-
-
-
Field Detail
-
writer
private final java.io.Writer writer
-
preference
private final CsvPreference preference
-
encoder
private final CsvEncoder encoder
-
lineNumber
private int lineNumber
-
rowNumber
private int rowNumber
-
columnNumber
private int columnNumber
-
-
Constructor Detail
-
AbstractCsvWriter
public AbstractCsvWriter(java.io.Writer writer, CsvPreference preference)Constructs a new AbstractCsvWriter with the supplied writer and preferences.- Parameters:
writer- the stream to write topreference- the CSV preferences- Throws:
java.lang.NullPointerException- if writer or preference are null
-
AbstractCsvWriter
public AbstractCsvWriter(java.io.Writer writer, CsvPreference preference, boolean bufferizeWriter)Constructs a new AbstractCsvWriter with the supplied writer, preferences and option to wrap the writer.- Parameters:
writer- the stream to write topreference- the CSV preferencesbufferizeWriter- indicates if the writer should be wrapped internally with a BufferedWriter- Throws:
java.lang.NullPointerException- if writer or preference are null
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses the underlying writer, flushing it first.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOExceptionFlushes the underlying writer.- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.io.IOException
-
incrementRowAndLineNo
protected void incrementRowAndLineNo()
In order to maintain the current row and line numbers, this method must be called at the very beginning of every write method implemented in concrete CSV writers. This will allow the correct row/line numbers to be used in any exceptions thrown before writing occurs (e.g. during CellProcessor execution), and means thatgetLineNumber()andgetRowNumber()can be called after writing to return the line/row just written.
-
getLineNumber
public int getLineNumber()
Gets the current position in the file. The first line of the file is line number 1.- Specified by:
getLineNumberin interfaceICsvWriter- Returns:
- the line number
-
getRowNumber
public int getRowNumber()
Gets the current row number (i.e. the number of CSV records - including the header - that have been written). This differs from the lineNumber, which is the number of real lines that have been written to the file. The first row is row 1 (which is typically the header row).- Specified by:
getRowNumberin interfaceICsvWriter- Returns:
- the current row number
-
writeRow
protected void writeRow(java.util.List<?> columns) throws java.io.IOExceptionWrites a List of columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
java.lang.IllegalArgumentException- if columns.size == 0java.io.IOException- If an I/O error occursjava.lang.NullPointerException- if columns is null
-
writeRow
protected void writeRow(java.lang.Object... columns) throws java.io.IOExceptionWrites one or more Object columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
java.lang.IllegalArgumentException- if columns.length == 0java.io.IOException- If an I/O error occursjava.lang.NullPointerException- if columns is null
-
writeRow
protected void writeRow(java.lang.String... columns) throws java.io.IOExceptionWrites one or more String columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
java.lang.IllegalArgumentException- if columns.length == 0java.io.IOException- If an I/O error occursjava.lang.NullPointerException- if columns is null
-
writeComment
public void writeComment(java.lang.String comment) throws java.io.IOExceptionWrites a single-line comment to the CSV file (the comment must already include any special comment characters e.g. '#' at start). Please note that comments are not part of RFC4180, so this may make your CSV file less portable.- Specified by:
writeCommentin interfaceICsvWriter- Parameters:
comment- the comment- Throws:
java.io.IOException- if an I/O error occurs
-
writeHeader
public void writeHeader(java.lang.String... header) throws java.io.IOExceptionWrites the header of the CSV file.- Specified by:
writeHeaderin interfaceICsvWriter- Parameters:
header- one or more header Strings- Throws:
java.io.IOException- if an I/O error occurs
-
-