将输出保存到文件

Saving output to files

我目前正在尝试将通过我的对象创建的值放入一个文件中。我的程序输出工作正常,我在另一个 class 方法中的计算工作正常。我只想将 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 class

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

你没有关闭你的流——它们是显式缓冲的。所以一切都在内存中,你永远不会刷新到磁盘。使用 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[][]);
    }
}

您似乎想要保存一个名为 matrixAppMatrixMult 对象。

为什么不序列化它?

MatrixMult 中实现 java.io.Serializable 然后调用 dout.writeObject(matrixApp).


编辑

根据您的评论,这里 simple example 关于如何 serialize/de-serialize。


如果你不想使用序列化,

I would just like to save the the output of matrixApp into a file.

假设"the the output"firstsecond矩阵,

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 的解释,将其放入带有资源的 try 中。