Java CSV File Save saveCSV(int[][] A, String fileName)

Here you can find the source of saveCSV(int[][] A, String fileName)

Description

Saves an int matrix to disk using in a Column Space Value (CSV) format.

License

Open Source License

Parameter

Parameter Description
A The matrix being saved.
fileName Name of the file its being saved at.

Exception

Parameter Description

Declaration

public static void saveCSV(int[][] A, String fileName)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  ww  .ja v a2 s .  co m*/
 * TopicUtils.java 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.
 *
 * TopicUtils.java 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., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

import java.io.*;

public class Main {
    /**
     * Saves a double matrix to disk using in a Column Space Value (CSV) format.
     *
     * @param A The matrix being saved.
     * @param fileName Name of the file its being saved at.
     * @throws java.io.IOException
     */
    public static void saveCSV(double[][] A, String fileName)
            throws IOException {
        int cols = A[0].length;
        boolean isMatrix = true;
        PrintStream fileStream = new PrintStream(fileName);
        for (int i = 0; i < A.length; i++) {
            if (A[i].length != cols) {
                isMatrix = false;
            }
            for (int j = 0; j < A[i].length; j++) {
                fileStream.print(A[i][j] + " ");
            }
            fileStream.println();
        }
        fileStream.close();
        if (isMatrix) {
            System.out.println("\nSaved " + A.length + "x" + A[0].length
                    + " matrix to " + fileName);
        } else {
            System.out.println("\nSaved " + A.length + " row 2d array to "
                    + fileName);
        }
    }

    /**
     * Saves an int matrix to disk using in a Column Space Value (CSV) format. 
     *
     * @param A The matrix being saved.
     * @param fileName Name of the file its being saved at.
     * @throws java.io.IOException
     */
    public static void saveCSV(int[][] A, String fileName)
            throws IOException {
        int cols = A[0].length;
        boolean isMatrix = true;
        PrintStream fileStream = new PrintStream(fileName);
        for (int i = 0; i < A.length; i++) {
            if (A[i].length != cols) {
                isMatrix = false;
            }
            for (int j = 0; j < A[i].length; j++) {
                fileStream.print(A[i][j] + " ");
            }
            fileStream.println();
        }
        fileStream.close();
        if (isMatrix) {
            System.out.println("\nSaved " + A.length + "x" + A[0].length
                    + " matrix to " + fileName);
        } else {
            System.out.println("\nSaved " + A.length + " row 2d array to "
                    + fileName);
        }
    }

    /**
     * Saves a double vector to disk using in a Column Space Value (CSV) format. 
     *
     * @param A The matrix being saved.
     * @param fileName Name of the file its being saved at.
     * @throws java.io.IOException
     */
    public static void saveCSV(double[] A, String fileName)
            throws IOException {
        PrintStream fileStream = new PrintStream(fileName);
        for (int i = 0; i < A.length; i++) {
            fileStream.print(A[i] + " ");
        }
        fileStream.close();
        System.out.println("\nSaved " + A.length + " row vector to "
                + fileName);
    }

    /**
     * Saves an int vector to disk using in a Column Space Value (CSV) format. 
     *
     * @param A The matrix being saved.
     * @param fileName Name of the file its being saved at.
     * @throws java.io.IOException
     */
    public static void saveCSV(int[] A, String fileName) throws IOException {
        PrintStream fileStream = new PrintStream(fileName);
        for (int i = 0; i < A.length; i++) {
            fileStream.print(A[i] + " ");
        }
        fileStream.close();
        System.out.println("\nSaved " + A.length + " row vector to "
                + fileName);
    }
}

Related

  1. saveCSV(double[][] A, String fileName)