CubeTwister 2.0alpha141 2011-10-13

ch.randelshofer.io
Class CSVWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.FilterWriter
          extended by ch.randelshofer.io.CSVWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class CSVWriter
extends java.io.FilterWriter

Write values into a comma separated value (CSV) stream.

EBNF rules for the CSV format:

 CSV = Record {RecordSeparator, Record}
 
 RecordSeparator = linebreak
 Record = Field {FieldSeparator, Field}
 
 FieldSeparator = {whitespace} comma {whitespace}
 Field = UnquotedField | DQuotedField
 
 UnquotedField = (simplechar) {{simplechar|space}, (simplechar)}
 
 DQuotedField = dquote (simplechar|stuffeddquote|linebreak|comma} dquote
 
 
 simplechar = (* every character except specialchar *)
 specialchar = linebreak | comma | dquote | whitespace | space
 linebreak = lf | cr | cr, lf
 comma = ','
 dquote = '"'
 stuffeddquote = '""'
 lf = 0x0a
 cr = 0x0d
 space = ' '
 whitespace = ' ' | tab
 tab = 0x07
 
 

Simple Example with unquoted fields:

 Jacques, Mayol, Rue St. Claire 8, Antibes
 Enzo, Maiorca, Via Greccio 2, Taormina
 
Example with quoted fields:
 Alice, "Did you go?
 Did you stay?", Rock
 The Pringles, "He said ""I like it""", Pop
 Trio, Uno, due, tre!, "Pop"
 

Version:
1.0.1 2011-10-13 New class comments.
1.0 2004-04-18 Created.
Author:
Werner Randelshofer

Field Summary
 
Fields inherited from class java.io.FilterWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
CSVWriter(java.io.Writer out)
          Creates a new instance.
CSVWriter(java.io.Writer out, char valueDelimiter, char quoteChar, java.lang.String recordDelimiter)
          Creates a new instance.
 
Method Summary
 void write(char[] cbuf, int off, int len)
          Write a portion of an array of characters and appends it as a new value to the current record.
 void write(int c)
          Write a single character value and appends it as a new value to the current record.
 void write(java.lang.String str)
          Write a string.
 void write(java.lang.String str, int off, int len)
          Write a portion of a string and appends it as a new value to the current record.
 void writeInt(int i)
          Write a single int value and appends it as a new value to the current record.
 void writeln()
          Finishes the current record by writing the record delimiter.
 void writeln(java.lang.String[] values)
          Writes a single record of comma separated values.
 
Methods inherited from class java.io.FilterWriter
close, flush
 
Methods inherited from class java.io.Writer
append, append, append, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVWriter

public CSVWriter(java.io.Writer out)
Creates a new instance.


CSVWriter

public CSVWriter(java.io.Writer out,
                 char valueDelimiter,
                 char quoteChar,
                 java.lang.String recordDelimiter)
Creates a new instance.

Parameters:
out - writer to which to print.
valueDelimiter - The new delimiter character to use.
quoteChar - The new character to use for quoting.
recordDelimiter - The line break used for delimiting records. Must be either cr, cr lf or lf.
Throws:
java.lang.IllegalArgumentException - if one of the delimiters can not be used.
Method Detail

writeInt

public void writeInt(int i)
              throws java.io.IOException
Write a single int value and appends it as a new value to the current record.

Throws:
java.io.IOException - If an I/O error occurs

write

public void write(int c)
           throws java.io.IOException
Write a single character value and appends it as a new value to the current record.

Overrides:
write in class java.io.FilterWriter
Throws:
java.io.IOException - If an I/O error occurs

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Write a portion of an array of characters and appends it as a new value to the current record.

Overrides:
write in class java.io.FilterWriter
Parameters:
cbuf - Buffer of characters to be written
off - Offset from which to start reading characters
len - Number of characters to be written
Throws:
java.io.IOException - If an I/O error occurs

write

public void write(java.lang.String str)
           throws java.io.IOException
Write a string.

Overrides:
write in class java.io.Writer
Parameters:
str - String to be written
Throws:
java.io.IOException - If an I/O error occurs

write

public void write(java.lang.String str,
                  int off,
                  int len)
           throws java.io.IOException
Write a portion of a string and appends it as a new value to the current record.

Overrides:
write in class java.io.FilterWriter
Parameters:
str - String to be written
off - Offset from which to start reading characters
len - Number of characters to be written
Throws:
java.io.IOException - If an I/O error occurs

writeln

public void writeln()
             throws java.io.IOException
Finishes the current record by writing the record delimiter.

Throws:
java.io.IOException

writeln

public void writeln(java.lang.String[] values)
             throws java.io.IOException
Writes a single record of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

Parameters:
values - values to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

(c) Werner Randelshofer.
All rights reserved.