com.devexperts.io
Class CSVWriter

java.lang.Object
  extended by com.devexperts.io.CSVWriter
All Implemented Interfaces:
Closeable, Flushable

public class CSVWriter
extends Object
implements Closeable, Flushable

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

CSVWriter

public CSVWriter(Writer writer)
Creates new CSVWriter with default separator and quote characters.

Throws:
NullPointerException - if writer is null

CSVWriter

public CSVWriter(Writer writer,
                 char separator,
                 char quote)
Creates new CSVWriter with specified separator and quote characters.

Throws:
NullPointerException - if writer is null
IllegalArgumentException - if separator or quote characters are invalid
Method Detail

getLineNumber

public int getLineNumber()
Returns current line number. Line numeration starts with 1 and counts new lines within record fields. Both CR and LF are counted as new lines, although CRLF sequence is counted only once. Line number points to new line after completion of the current record.


getRecordNumber

public int getRecordNumber()
Returns current record number. Record numeration starts with 1 and does not count new lines within record fields. Record number points to new record after completion of the current record.


writeField

public void writeField(String field)
                throws IOException
Writes specified field to the end of current record. Empty and null strings are written as empty fields. This method does not advance to the next record - it only adds field to the current record.

Throws:
IOException - If an I/O error occurs

writeRecord

public void writeRecord(String[] record)
                 throws IOException
Writes specified record and advances to the next record upon completion. Empty and null arrays are normally prohibited because records must contain at least one field, but they can be used to complete current record. If there is incomplete record (written by 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.

Throws:
IllegalArgumentException - if attempt to write record without fields was made
IOException - If an I/O error occurs

writeAll

public void writeAll(List<String[]> records)
              throws IOException
Writes specified records to the output. Does nothing if specified list is empty. Empty and null arrays are normally prohibited because records must contain at least one field, but they can be used to complete current record. If there is incomplete record (written by 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.

Throws:
IllegalArgumentException - if attempt to write record without fields was made
IOException - If an I/O error occurs

flush

public void flush()
           throws IOException
Flushes the stream.

Specified by:
flush in interface Flushable
Throws:
IOException - If an I/O error occurs

close

public void close()
           throws IOException
Closes the stream.

Specified by:
close in interface Closeable
Throws:
IOException - If an I/O error occurs


Copyright © 2013 Devexperts. All Rights Reserved.