public class CSVWriter extends Object implements Closeable, Flushable
This writer supports records with arbitrary (variable) number of fields, multiline fields, custom separator and quote characters. It uses CRLF sequence to separate records.
This writer does not provide buffering of any sort and does not perform encoding. The correct way to efficiently write CSV file with UTF-8 encoding is as follows:
CSVWriter writer = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8))); writer.writeRecord(header); writer.writeAll(records); writer.close();
Constructor and Description |
---|
CSVWriter(Writer writer)
Creates new CSVWriter with default separator and quote characters.
|
CSVWriter(Writer writer,
char separator,
char quote)
Creates new CSVWriter with specified separator and quote characters.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the stream.
|
void |
flush()
Flushes the stream.
|
int |
getLineNumber()
Returns current line number.
|
int |
getRecordNumber()
Returns current record number.
|
void |
writeAll(List<String[]> records)
Writes specified records to the output.
|
void |
writeField(String field)
Writes specified field to the end of current record.
|
void |
writeRecord(String[] record)
Writes specified record and advances to the next record upon completion.
|
public CSVWriter(Writer writer)
NullPointerException
- if writer is nullpublic CSVWriter(Writer writer, char separator, char quote)
NullPointerException
- if writer is nullIllegalArgumentException
- if separator or quote characters are invalidpublic int getLineNumber()
public int getRecordNumber()
public void writeField(String field) throws IOException
IOException
- If an I/O error occurspublic void writeRecord(String[] record) throws IOException
writeField(java.lang.String)
method)
then specified fields will be added to the end of current record,
the record will be completed and writer will advance to the next record.IllegalArgumentException
- if attempt to write record without fields was madeIOException
- If an I/O error occurspublic void writeAll(List<String[]> records) throws IOException
writeField(java.lang.String)
method)
then fields from first specified record will be added to the end of current record,
the record will be completed and writer will advance to the next record.IllegalArgumentException
- if attempt to write record without fields was madeIOException
- If an I/O error occurspublic void flush() throws IOException
flush
in interface Flushable
IOException
- If an I/O error occurspublic void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
- If an I/O error occursCopyright © 2002–2025 Devexperts LLC. All rights reserved.