Saves a Swing table model to a comma separated variable file - Java Swing

Java examples for Swing:JTable Model

Description

Saves a Swing table model to a comma separated variable file

Demo Code


//package com.java2s;
import java.io.*;
import javax.swing.table.*;

public class Main {
    /**/* w w  w  .  ja v  a2s  .c  o m*/
     * Saves a table model to a comma separated variable file
     * @param file File to save data to
     * @param data Data to save
     * @throws java.io.IOException thrown if an IOException occurs
     */
    public static void saveCSVFile(File file, TableModel data)
            throws IOException {
        StringBuilder dataBuffer = new StringBuilder();

        // Gets the column headers
        for (int columnIndex = 0; columnIndex < data.getColumnCount(); columnIndex++) {
            dataBuffer.append(data.getColumnName(columnIndex) + ",");
        }

        dataBuffer.append("\n");

        // Gets the table data
        for (int row = 0; row < data.getRowCount(); row++) {
            for (int column = 0; column < data.getColumnCount(); column++) {
                dataBuffer.append(data.getValueAt(row, column) + ",");
            }

            dataBuffer.append("\n");
        }

        // Writes the data to the specified file
        FileOutputStream output = new FileOutputStream(file);
        output.write(dataBuffer.toString().getBytes());
        output.close();
    }
}

Related Tutorials