Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.mindprod.csv
Class CSVWriter

    java.lang.Object
      com.mindprod.csv.CSVWriter

    public final class CSVWriter

extends java.lang.Object

Write CSV (Comma Separated Value) files.

This format is used my Microsoft Word and Excel. Fields are separated by commas, and enclosed in quotes if they contain commas or quotes. Embedded quotes are doubled. Embedded spaces do not normally require surrounding quotes. The last field on the line is not followed by a comma. Null fields are represented by two commas in a row.

Must be combined with your own code.

Since:
  1998
Version:
  3.0 2009-06-15 - lookup table to speed CSVReader
Author:
  Roedy Green, Canadian Mind Products

Constructor Summary
`CSVWriter(java.io.PrintWriter pw)` convenience Constructor, defaults to quotelevel 1, comma separator , trim
`CSVWriter(java.io.PrintWriter pw, int quoteLevel, char separatorChar, char quoteChar, char commentChar, boolean trim)` Constructor

Method Summary
`void` | `close()` Close the PrintWriter.
`static void` | `main(java.lang.String[] args)` Test driver
`void` | `nl()` Write a new line in the CVS output file to demark the end of record.
`void` | `nl(java.lang.String comment)` Write a comment followed by new line in the CVS output file to demark the end of record.
`void` | `put(boolean b)` Write one boolean field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(char c)` Write one csv field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(double d)` Write one csv field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(float f)` Write one csv field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(int i)` Write one csv field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(long l)` Write one csv field to the file, followed by a separator unless it is the last field on the line.
`void` | `put(java.lang.String s)` Write one csv field to the file, followed by a separator unless it is the last field on the line.

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.PrintWriter pw)

  convenience Constructor, defaults to quotelevel 1, comma separator , trim

  Parameters:
    `pw` - Buffered PrintWriter where fields will be written

### CSVWriter

    public CSVWriter(java.io.PrintWriter pw,
                     int quoteLevel,
                     char separatorChar,
                     char quoteChar,
                     char commentChar,
                     boolean trim)

  Constructor

  Parameters:
    `pw` - Buffered PrintWriter where fields will be written
    `quoteLevel` - -1 = like 0, but add an extra space after each separator/comma, 0 = minimal quotes, only around fields containing quotes or separators. 1 = quotes also around fields containing spaces. 2 = quotes around all fields, whether or not they contain commas, quotes or spaces.
    `separatorChar` - field separator character, usually ',' in North America, ';' in Europe and sometimes '\t' for tab. Note this is a 'char' not a "string".
    `quoteChar` - char to use to enclose fields containing a separator, usually '\"'. Use (char)0 if you don't want a quote character. Note this is a 'char' not a "string".
    `commentChar` - char to prepend on any comments you write. usually ; or #. Note this is a 'char' not a "string".
    `trim` - true if writer should trim leading/trailing whitespace (e.g. blank, cr, Lf, tab) before writing the field.

Method Detail

### close

    public void close()

  Close the PrintWriter.

### nl

    public void nl()

  Write a new line in the CVS output file to demark the end of record.

### nl

    public void nl(java.lang.String comment)

  Write a comment followed by new line in the CVS output file to demark the end of record.

  Parameters:
    `comment` - comment string containing any chars. Lead comment character will be applied automatically.

### put

    public void put(int i)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `i` - The int to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(char c)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `c` - The char to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(boolean b)

  Write one boolean field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `b` - The boolean to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(long l)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `l` - The long to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(double d)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `d` - The double to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(float f)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

  Parameters:
    `f` - The float to write. Any additional quotes or embedded quotes will be provided by put.

### put

    public void put(java.lang.String s)

  Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed. Don't use this method to write comments.

  Parameters:
    `s` - The string to write. Any additional quotes or embedded quotes will be provided by put. Null means start a new line.
  See Also:
    `nl(String)`

### main

    public static void main(java.lang.String[] args)

  Test driver

  Parameters:
    `args` - not used

Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD