Saving output to files

I currently am trying to put the values that are created through my object into a file. My output for the program works fine and my computations in another class's methods work. I would just like to save the the output of matrixApp into a file. I also show what my output looks like without trying to save to a file. The route that I have taken doesn't seem to be working, any suggestions?

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
  }
}

The 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");
        }
  }
}

The output of matrixApp is

Product of entered matrices:-

1 -2 -4
-1 7 -18

7 1 5
Sum of entered matrices:-

4 0 -3
4 4 -5
2 -1 7
Transpose of first entered matrix:-

2 1 1
-1 0 1
0 -3 2
Transpose of second entered matrix:-

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

Jon Skeet
people
quotationmark

You're not closing your streams - which are explicitly buffered. So everything is in memory, and you never gets flushed to disk. Use a try-with-resources statement to close everything appropriately, even if an exception is thrown.:

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[][]);
    }
}

people

See more on this question at Stackoverflow