Java BufferedWriter Write writeFile(double[][] toWrite, String outName)

Here you can find the source of writeFile(double[][] toWrite, String outName)

Description

Write a double[][] with an arbitrary number of columns to a file.

License

Open Source License

Parameter

Parameter Description
toWrite is the double[][] to write
outName is the full output path

Return

true if successfully completed.

Declaration

public static boolean writeFile(double[][] toWrite, String outName) 

Method Source Code


//package com.java2s;

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    /**//from   w ww .j av a  2  s  .com
     * Write a double[][] with an arbitrary number of columns to a file.
     * @param toWrite is the double[][] to write
     * @param outName is the full output path
     * @return true if successfully completed.
     */
    public static boolean writeFile(double[][] toWrite, String outName) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(new File(outName)));
            for (int l = 0; l < toWrite[0].length; l++) {
                String line = "";
                for (int c = 0; c < toWrite.length; c++) {
                    if (c == toWrite.length - 1) {
                        line += toWrite[c][l];
                    } else {
                        line += toWrite[c][l] + ",";
                    }
                }
                writer.write(line);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * Write an int[][] with an arbitrary number of columns to a file.
     * @param toWrite is an int[][] to write
     * @param outName is the full output path
     * @return true if successfully completed.
     */
    public static boolean writeFile(int[][] toWrite, String outName) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(new File(outName)));
            for (int l = 0; l < toWrite[0].length; l++) {
                String line = "";
                for (int c = 0; c < toWrite.length; c++) {
                    if (c == toWrite.length - 1) {
                        line += toWrite[c][l];
                    } else {
                        line += toWrite[c][l] + ",";
                    }
                }
                writer.write(line);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * Write an int[][] with an arbitrary number of columns to a file.
     * @param toWrite is an int[][] to write
     * @param outName is the full output path
     * @return true if successfully completed.
     */
    public static boolean writeFile(String[][] toWrite, String outName) {
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(new File(outName)));
            for (int l = 0; l < toWrite[0].length; l++) {
                String line = "";
                for (int c = 0; c < toWrite.length; c++) {
                    if (c == toWrite.length - 1) {
                        line += toWrite[c][l];
                    } else {
                        line += toWrite[c][l] + ",";
                    }
                }
                writer.write(line);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }
}

Related

  1. writeFile(BufferedWriter fw, String line)
  2. writeFile(File dir, File fileRef, File newFileName, ClassLoader classLoader)
  3. writeFile(File src, File dst)
  4. writeFile(Object msg)
  5. writeFile(String dir, String file, String content)