/*
  http://opencsv.sourceforge.net/ :

  CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
     // feed in your array (or convert your data to an array)
     String[] entries = "first#second#third".split("#");
     writer.writeNext(entries);
	 writer.close();
*/

package org.opentreeoflife.taxa;

import java.io.Writer;
import java.io.IOException;

public class CSVWriter {

    Writer writer;

    public CSVWriter(Writer writer) {
        this.writer = writer;
    }

    public void writeNext(String[] fields) throws IOException {
        StringBuffer sb = new StringBuffer();
        int nfields = fields.length;
        for (int i = 0; i < nfields; ++i) {
            if (i > 0)
                sb.append(',');
            if (fields[i] == null)
                ;
            else if (fields[i].indexOf(',') >= 0 || fields[i].indexOf('"') >= 0) {
                sb.append('"');
                sb.append(fields[i].replace("\"", "\"\""));
                sb.append('"');
            } else
                sb.append(fields[i]);
        }
        sb.append('\n');
        writer.write(sb.toString());
    }

    public void close() throws IOException {
        writer.close();
    }

}

@@ -479,32 +479,254 @@
  writer;%0A    %7D%0A%0A
+    static final char SEP = ',';%0A    static final char QUOTE = '%22';%0A    static final String QUOTE_STRING = new String(new char%5B%5D%7BQUOTE%7D);%0A    static final String QUOTE_QUOTE_STRING = new String(new char%5B%5D%7BQUOTE, QUOTE%7D);%0A%0A
     public void 
@@ -789,18 +789,19 @@
 StringBu
-ff
+ild
 er sb = 
@@ -816,10 +816,11 @@
 ngBu
-ff
+ild
 er()
@@ -951,19 +951,19 @@
 .append(
-','
+SEP
 );%0A     
@@ -1049,19 +1049,19 @@
 indexOf(
-','
+SEP
 ) %3E= 0 %7C
@@ -1080,19 +1080,21 @@
 indexOf(
-'%22'
+QUOTE
 ) %3E= 0) 
@@ -1113,35 +1113,37 @@
       sb.append(
-'%22'
+QUOTE
 );%0A             
@@ -1177,20 +1177,40 @@
 ace(
-%22%5C%22%22, %22%5C%22%5C%22%22
+QUOTE_STRING, QUOTE_QUOTE_STRING
 ));%0A
@@ -1235,19 +1235,21 @@
 .append(
-'%22'
+QUOTE
 );%0A     

make csvwriter code slightly more abstract