Class AbstractCsvWriter
java.lang.Object
org.supercsv.io.AbstractCsvWriter
- All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, ICsvWriter
- Direct Known Subclasses:
CsvBeanWriter, CsvListWriter, CsvMapWriter, CsvResultSetWriter
Defines the standard behaviour of a CSV writer.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intprivate final CsvEncoderprivate intprivate final CsvPreferenceprivate intprivate final Writer -
Constructor Summary
ConstructorsConstructorDescriptionAbstractCsvWriter(Writer writer, CsvPreference preference) Constructs a new AbstractCsvWriter with the supplied writer and preferences.AbstractCsvWriter(Writer writer, CsvPreference preference, boolean bufferizeWriter) Constructs a new AbstractCsvWriter with the supplied writer, preferences and option to wrap the writer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying writer, flushing it first.voidflush()Flushes the underlying writer.intGets the current position in the file.intGets the current row number (i.e.protected voidIn 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(String comment) Writes a single-line comment to the CSV file (the comment must already include any special comment characters e.g.voidwriteHeader(String... header) Writes the header of the CSV file.protected voidWrites one or more Object columns as a line to the CsvWriter.protected voidWrites one or more String columns as a line to the CsvWriter.protected voidWrites a List of columns as a line to the CsvWriter.
-
Field Details
-
writer
-
preference
-
encoder
-
lineNumber
private int lineNumber -
rowNumber
private int rowNumber -
columnNumber
private int columnNumber
-
-
Constructor Details
-
AbstractCsvWriter
Constructs a new AbstractCsvWriter with the supplied writer and preferences.- Parameters:
writer- the stream to write topreference- the CSV preferences- Throws:
NullPointerException- if writer or preference are null
-
AbstractCsvWriter
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:
NullPointerException- if writer or preference are null
-
-
Method Details
-
close
Closes the underlying writer, flushing it first.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
flush
Flushes the underlying writer.- Specified by:
flushin interfaceFlushable- Throws:
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
Writes a List of columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
IllegalArgumentException- if columns.size == 0IOException- If an I/O error occursNullPointerException- if columns is null
-
writeRow
Writes one or more Object columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
IllegalArgumentException- if columns.length == 0IOException- If an I/O error occursNullPointerException- if columns is null
-
writeRow
Writes one or more String columns as a line to the CsvWriter.- Parameters:
columns- the columns to write- Throws:
IllegalArgumentException- if columns.length == 0IOException- If an I/O error occursNullPointerException- if columns is null
-
writeComment
Writes 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:
IOException- if an I/O error occurs
-
writeHeader
Writes the header of the CSV file.- Specified by:
writeHeaderin interfaceICsvWriter- Parameters:
header- one or more header Strings- Throws:
IOException- if an I/O error occurs
-