|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.devexperts.io.CSVWriter
public class CSVWriter
Writes data to the stream using Comma-Separated Values (CSV) format. See RFC 4180 for CSV format specification.
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), "UTF-8"))); writer.writeRecord(header); writer.writeAll(records); writer.close();
Constructor Summary | |
---|---|
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. |
Method Summary | |
---|---|
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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public CSVWriter(Writer writer)
NullPointerException
- if writer is nullpublic CSVWriter(Writer writer, char separator, char quote)
NullPointerException
- if writer is null
IllegalArgumentException
- if separator or quote characters are invalidMethod Detail |
---|
public 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 made
IOException
- 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 made
IOException
- 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
IOException
- If an I/O error occurs
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |