将输出保存到文件

时间:2015-04-24 16:25:29

标签: java file dataoutputstream

我目前正在尝试将通过我的对象创建的值放入文件中。我的程序输出工作正常,我在另一个类的方法中的计算工作。我只想将matrixApp的输出保存到文件中。我还显示了我的输出看起来没有尝试保存到文件。我采取的路线似乎没有用,有什么建议吗?

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.util.Scanner;


public class MatrixApp {

  public static void main(String[] args) {
    int m, n, p, q;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of rows and columns of first matrix");
    m = input.nextInt();
    n = input.nextInt();

    System.out.println("Enter the number of rows and columns of second matrix");
    p = input.nextInt();
    q = input.nextInt();

    System.out.println("Enter the elements of first matrix");

    int first[][] = new int[m][n];
    for (int c = 0; c < m; c++)
        for (int d = 0; d < n; d++)
            first[c][d] = input.nextInt();

    System.out.println("Enter the elements of second matrix");
    int second[][] = new int[p][q];

    for (int c = 0; c < p; c++)
        for (int d = 0; d < q; d++)
            second[c][d] = input.nextInt();



    MatrixMult matrixApp = new MatrixMult(first, second, m, n, p, q);

    //String filename = "data.txt";
    //FileOutputStream fout = new FileOutputStream(filename);
    //BufferedOutputStream bout = new BufferedOutputStream(fout);
    //DataOutputStream dout = new DataOutputStream(bout);
    //for (int i=0; i < matrixApp; i++){ <--- the problem is here and 
        //dout.writeInt(matrixApp[][]);   getting the program to do all the 
    //}                                   the outputs
  }
}

MatrixMult类

public class MatrixMult{

public MatrixMult(int first[][], int second[][], int m, int n, int p, int q) {
    doMatrixMultiply(first, second, m, n, p, q);
}

public void doMatrixMultiply(int first[][], int second[][], int m, int n, int p, int q) {

    if (n != p)
        System.out
                .println("Matrices with entered orders can't be multiplied with each other.");
    else {
        int multiply[][] = new int[m][q];
        int addition[][] = new int[m][q];
        int transpose[][] = new int[m][q];
        int transpose2[][] = new int[m][q];

        int mult = 0;
        int sum = 0;
        int tran = 0;


        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++) {
                for (int k = 0; k < p; k++) {
                    mult = mult + first[c][k] * second[k][d];
                }

                multiply[c][d] = mult;
                mult = 0;
            }
        }

        System.out.println("Product of entered matrices:-");

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++)
                System.out.print(multiply[c][d] + "\t");

            System.out.print("\n");
        }

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++) {
                for (int k = 0; k < p; k++) {
                    sum = first[c][d] + second[c][d];
                }

                addition[c][d] = sum;
                sum = 0;
            }
        }

        System.out.println("Sum of entered matrices:-");

        for (int c = 0; c < m; c++) {
            for (int d = 0; d < q; d++)
                System.out.print(addition[c][d] + "\t");

            System.out.print("\n");
        }

        int c;
        int d;
        for (c = 0; c < m; c++) {
            for (d = 0; d < q; d++)
                transpose[d][c] = first[c][d];
        }
        for (c = 0; c < m; c++) {
            for (d = 0; d < q; d++)
                transpose2[d][c] = second[c][d];
        }

        System.out.println("Transpose of first entered matrix:-");

        for (c = 0; c < n; c++) {
            for (d = 0; d < m; d++)
                System.out.print(transpose[c][d] + "\t");

            System.out.print("\n");
        }

        System.out.println("Transpose of second entered matrix:-");

        for (c = 0; c < n; c++) {
            for (d = 0; d < m; d++)
                System.out.print(transpose2[c][d] + "\t");

            System.out.print("\n");
        }
  }
}

matrixApp的输出是

输入矩阵的产品: -

1 -2 -4
-1 7 -18

7 1 5
输入矩阵的总和: -

4 0 -3
4 4 -5
2 -1 7
转置第一个输入的矩阵: -

2 1 1 -1 0 1
0 -3 2
第二个输入矩阵的转置: -

2 3 1
1 4 -2
-3 -2 5

2 个答案:

答案 0 :(得分:2)

您没有关闭您的流 - 显式缓冲。所以一切都在内存中,你永远不会被冲到磁盘上。使用try-with-resources语句适当地关闭所有内容,即使抛出异常。:

try (FileOutputStream fout = new FileOutputStream(filename);
     BufferedOutputStream bout = new BufferedOutputStream(fout);
     DataOutputStream dout = new DataOutputStream(bout)) {
    // Note: Body here is as per original question. It won't compile, but
    // we don't know what was expected.
    for (int i=0; i < matrixApp; i++){
        dout.writeInt(matrixApp[][]);
    }
}

答案 1 :(得分:1)

好像您要保存名为MatrixMult的{​​{1}}对象。

为什么不序列化呢?

matrixApp中实施java.io.Serializable,然后致电MatrixMult

修改

根据您的评论,这里有关于如何序列化/反序列化的simple example

如果您不想使用序列化,

  

我只想将matrixApp的输出保存到文件中。

假设“输出”dout.writeObject(matrixApp)first矩阵,

second

将其放在带有ressource的for (int i=0; i < first.length; i++){ for(int j = 0; k < first[i].length; j++) dout.writeInt(first[i][j]); for (int i=0; i < second.length; i++){ for(int j = 0; k < second[i].length; j++) dout.writeInt(second[i][j]); 中,如@JonSkeet所解释。