package CSV; import java.util.*; import java.io.*; public class CSV { public List header = new LinkedList() ; public LinkedList rows = new LinkedList() ; public void addHeaderItem(Object h) { header.add(h) ; } public void addItem(Object i) { if (rows.isEmpty()) { rows.add(new LinkedList()) ; } rows.getLast().add(i) ; } public void startNewRow() { rows.add(new LinkedList()) ; } private void printRow(PrintStream outfile, List row){ if (row != null && ! row.isEmpty()) { Iterator iter = row.iterator() ; outfile.print(iter.next().toString()) ; while (iter.hasNext()) { outfile.print(", " + iter.next()) ; } outfile.println("") ; } } public void save(String fname) { PrintStream outfile = null; try { outfile = new PrintStream(new FileOutputStream(fname)); } catch (IOException e) { throw new Error(e) ; } printRow(outfile,header) ; for (List row : rows) printRow(outfile,row) ; outfile.close() ; } }