猿问

如何在文本文件中保存矩阵

我只想在文本文件中输入矩阵,但结果明显不同。我没有任何想法。


public void saveToTextFile() {

    try {

        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("matrix.txt")));


        for (int i = 0; i < matrix.length; i++) {

            for (int j = 0; j < matrix[i].length; j++) {

                writer.write(matrix[i][j] + " ");

            }

            writer.newLine();

        }

        writer.flush();

        writer.close();


    } catch (IOException e) {

        System.out.println("Error");

    }

}

我预计


1 2 3

4 5 6

7 8 9

但在文件中


1 1 1

5 5 5

9 9 9


拉风的咖菲猫
浏览 709回答 2
2回答

慕容3067478

你可以试试这个:int[][]&nbsp;ints&nbsp;=&nbsp;new&nbsp;int[4][4];&nbsp;//&nbsp;Let's&nbsp;say&nbsp;you&nbsp;have&nbsp;a&nbsp;4&nbsp;*&nbsp;4&nbsp;ints&nbsp;array&nbsp;filled&nbsp;like&nbsp;this &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[0][0]&nbsp;=&nbsp;1; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[0][1]&nbsp;=&nbsp;2; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[0][2]&nbsp;=&nbsp;3; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[0][3]&nbsp;=&nbsp;4; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[1][0]&nbsp;=&nbsp;5; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[1][1]&nbsp;=&nbsp;6; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[1][2]&nbsp;=&nbsp;7; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[1][3]&nbsp;=&nbsp;8; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[2][0]&nbsp;=&nbsp;9; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[2][1]&nbsp;=&nbsp;10; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[2][2]&nbsp;=&nbsp;11; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[2][3]&nbsp;=&nbsp;12; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[3][0]&nbsp;=&nbsp;13; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[3][1]&nbsp;=&nbsp;14; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[3][2]&nbsp;=&nbsp;15; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ints[3][3]&nbsp;=&nbsp;16; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuilder&nbsp;sb&nbsp;=&nbsp;new&nbsp;StringBuilder();&nbsp;//&nbsp;String&nbsp;Builder&nbsp;to&nbsp;create&nbsp;the&nbsp;table&nbsp;structure&nbsp;before&nbsp;writing&nbsp;it&nbsp;to&nbsp;the&nbsp;file. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int[]&nbsp;int1&nbsp;:&nbsp;ints)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;j&nbsp;=&nbsp;0;&nbsp;j&nbsp;<&nbsp;int1.length;&nbsp;j++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append(int1[j]).append("\t");&nbsp;//&nbsp;Add&nbsp;tab&nbsp;to&nbsp;delimite&nbsp;the&nbsp;elements &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sb.append("\r\n");&nbsp;//&nbsp;Add&nbsp;new&nbsp;line&nbsp;character &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(sb); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Path&nbsp;path&nbsp;=&nbsp;Paths.get("C:\\Users\\youruser\\Documents\\test.txt");&nbsp;//&nbsp;The&nbsp;path&nbsp;to&nbsp;your&nbsp;file &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Files.write(path,&nbsp;sb.toString().getBytes());&nbsp;//&nbsp;Writes&nbsp;to&nbsp;that&nbsp;path&nbsp;the&nbsp;bytes&nbsp;in&nbsp;the&nbsp;string&nbsp;from&nbsp;the&nbsp;stringBuilder&nbsp;object.这将打印像表格的值:

繁星淼淼

对您的方法进行轻微修改:try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[][] matrix = new int[3][3];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedWriter writer = new BufferedWriter(new FileWriter(new File("matrix.txt")));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int num = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < matrix.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < matrix[i].length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.write(num + " ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; num++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.newLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.flush();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.close();&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Error");&nbsp; &nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答