CSV Writer : CSV File « Development Class « Java






CSV Writer

  
/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, info@pearreview.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

//package com.pearreview.util.csv;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;

public class CSVWriter implements Serializable {
  private static final long serialVersionUID = 2444199148253681816L;

  public static final char COMMA_SEPARATOR = 0x2C;
  public static final char COLON_SEPARATOR = 0x3A;
  public static final char SEMICOLON_SEPARATOR = 0x3B;
  public static final char TABULATOR_SEPARATOR = 0x09;

  /**
   * Writes a table model to csv formatted file
   * 
   * @param file
   *            file to create
   * @param model
   *            model to write
   * @throws IOException
   */
  public void write(File file, CSVTableModel model, char separator)
      throws IOException {
    /* create file */
    FileWriter fw = new FileWriter(file);
    BufferedWriter bw = new BufferedWriter(fw);

    String value;

    /* write columns */
    if (model.areColumnsVisible()) {
      for (int column = 0; column < model.getColumnCount(); column++) {
        value = encodeValue(model.getColumnName(column), separator);
        bw.write(value);
        if (column < model.getColumnCount() - 1) {
          bw.write(separator);
        } else {
          bw.newLine();
        }
      }
    }

    /* write data */
    for (int row = 0; row < model.getRowCount(); row++) {
      for (int column = 0; column < model.getColumnCount(); column++) {
        value = encodeValue(model.getValueAt(row, column), separator);
        bw.write(value);
        if (column < model.getColumnCount() - 1) {
          bw.write(separator);
        } else {
          bw.newLine();
        }
      }
    }

    /* close file */
    bw.close();
  }

  protected String encodeValue(String value, char separator) {
    return "\"" + value + "\"";
  }
}

interface CSVTableModel {
  public String getValueAt(int row, int column);

  public int getColumnCount();

  public int getRowCount();

  public String getColumnName(int column);

  public boolean areColumnsVisible();
}

   
    
  








Related examples in the same category

1.A utility class that parses a Comma Separated Values (CSV) file
2.Simple demo of CSV parser classSimple demo of CSV parser class
3.CSV in action: lines from a file and printCSV in action: lines from a file and print
4.Simple demo of CSV matching using Regular Expressions
5.Helper class to write table data to a csv-file (comma separated values).
6.Builds a bracketed CSV list from the array
7.Builds a CSV list from the specified String[], separator string and quote string
8.Builds a CSV list from the specified two dimensional String[][], separator string and quote string.
9.The csv tokenizer class allows an application to break a Comma Separated Value format into tokens.
10.The CSVQuoter is a helper class to encode a string for the CSV file format.
11.A stream based parser for parsing delimited text data from a file or a stream
12.Reads CSV (Comma Separated Value) files
13.Writes CSV (Comma Separated Value) files
14.Csv Converter
15.CVS reader
16.CSV Writer
17.CSV parser
18.Csv Reader
19.A very simple CSV parser released under a commercial-friendly license.
20.A very simple CSV reader released under a commercial-friendly license.
21.A very simple CSV writer released under a commercial-friendly license.
22.CSV file reader
23.CSV file writer
24.CSV Tokenizer Util
25.Parse a line of text in CSV format and returns array of Strings Implementation of parsing is extracted from open-csv.
26.Parse comma-separated list of ints and return as array
27.Parse comma-separated list of longs and return as array