public class CSVWriter extends AbstractTableWriter
Implementation of TableWriter
for CSV files.
Constructor and Description |
---|
CSVWriter(java.io.Writer w,
TableLayout l)
Construct a new CSV writer.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Finish the table.
|
void |
flush()
Flush the writer, causing all currently-written rows to be flushed to output.
|
TableLayout |
getLayout()
Get the layout of this table.
|
static CSVWriter |
open(java.io.File file,
TableLayout layout)
Open a CSV writer to write to an auto-compressed file.
|
static CSVWriter |
open(java.io.File file,
TableLayout layout,
CompressionMode compression)
Open a CSV writer to write to a file.
|
void |
writeRow(java.util.List<?> row)
Write a row to the table.
|
checkRowWidth, writeRow
public CSVWriter(@WillCloseWhenClosed @Nonnull java.io.Writer w, @Nullable TableLayout l) throws java.io.IOException
Construct a new CSV writer.
w
- The underlying writer to output to.l
- The table layout, or null
if the table has no headers.java.io.IOException
- if there is an error writing the column headers.public void flush() throws java.io.IOException
TableWriter
Flush the writer, causing all currently-written rows to be flushed to output.
java.io.IOException
- if an error occurs while flushing output.public void close() throws java.io.IOException
TableWriter
Finish the table. Depending on how it was constructed, some underlying resource may be closed.
java.io.IOException
public void writeRow(java.util.List<?> row) throws java.io.IOException
TableWriter
Write a row to the table. This method is thread-safe.
row
- A row of values. If the table requires more columns, the remaining columns are empty. The row is copied if necessary; the caller is free to re-use the same array for returnValue calls.java.io.IOException
- if an error occurs writing the row.public TableLayout getLayout()
TableWriter
Get the layout of this table.
public static CSVWriter open(java.io.File file, TableLayout layout, CompressionMode compression) throws java.io.IOException
Open a CSV writer to write to a file.
file
- The file to write to.layout
- The layout of the table.compression
- What compression, if any, to use.file
.java.io.IOException
- if there is an error opening the file or writing the column header.public static CSVWriter open(java.io.File file, @Nullable TableLayout layout) throws java.io.IOException
Open a CSV writer to write to an auto-compressed file. The file will be compressed if its name ends in “.gz”.
file
- The file.layout
- The table layout.java.io.IOException
- if there is an error opening the file or writing the column header.open(File, TableLayout, CompressionMode)