首页 > 解决方案 > 在 .txt 文件中保存一个巨大的矩阵会弄乱换行符

问题描述

我正在通过 PrintWriter 保存 3500 x 2000 矩阵。

    float[][] nodeSH = new float[3500][2000];
    String fullPath = path + name + ".txt";
    File file = new File(fullPath);

    try {
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        PrintWriter out = new PrintWriter(fullPath);
        int sizeX = nodeSH.length;
        int sizeY = nodeSH[0].length;
        out.println(sizeX + "," + sizeY);
        String output = "";

        for (int x = 0; x < sizeX; x++) {
            output = "";
            for (int y = 0; y < sizeY; y++) {
                output = output + nodeSH[x][y] + ",";
            }
            output = output.substring(0, output.length() - 1);
            out.println(output);

        }
        out.flush();
        out.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

当我打开文本文件时,它工作得很好,每 9 行比其他行短一点。

例如看我的:文本文件图片

标签: javamatrixsavetext-files

解决方案


推荐阅读