[英]Saving output to files
我目前正在嘗試將通過我的對象創建的值放入文件中。 我的程序輸出工作正常,我在另一個類的方法中的計算工作。 我只想將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
你沒有關閉你的流 - 明確緩沖。 所以一切都在內存中,你永遠不會被沖到磁盤上。 使用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[][]);
}
}
好像你想要保存一個名為matrixApp
的MatrixMult
對象。
為什么不序列化呢?
在MatrixMult
實現java.io.Serializable
,然后調用dout.writeObject(matrixApp)
。
編輯
根據您的評論,這里有一個關於如何序列化/反序列化的簡單示例 。
如果你不想使用序列化,
我只想將matrixApp的輸出保存到文件中。
假設“輸出”是第first
和second
矩陣,
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解釋的那樣,將它與ressource一起try
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.