CSV Writer : CSV File « Development Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Development Class » CSV File 




CSV Writer
  
/*
PearReview - The peer review assistant.
Copyright (C) 2009  Dimitrij Pankratz, Anton Musichin
http://www.pearreview.com, [email protected]

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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.