0

我有这个代码,

else if (e.getSource() == saveButton) {
            Scanner scan;
            String es = " ";
            try {
                int status = chooser.showSaveDialog(null);
                if (status == JFileChooser.APPROVE_OPTION) {
                    File newImageName = chooser.getSelectedFile();
                    PrintWriter outFile = new PrintWriter(new FileWriter(newImageName));
                    outFile.print(drawing.typefile);
                    outFile.print(drawing.iname);
                    outFile.print(drawing.width);
                    outFile.print(es);
                    outFile.print(drawing.height);
                    outFile.print(drawing.maxshade);
                    for(int r = 0; r < drawing.array.length; r++)
                        for(int c = 0; c < drawing.array[r].length; c++)
                            outFile.print(drawing.array[r][c]);


            //      outFile.print(drawing.paintComponent(newImageName));
                    outFile.flush();
                    outFile.close();
                }
             else if (status == JFileChooser.CANCEL_OPTION) {
                }
            } catch (Exception ex) {
                    JOptionPane.showMessageDialog(null, "File could not be written, try again.");
                    //ex.printStackTrace();
                }

        }

以及如何让它在值之间打印空格?它打印

P2image1.pgm320 275255132132130125125128

我需要它来打印

P2image1.pgm320 275255132 132 130 125 125 128

因此每个数组值之间都有一个空格,我尝试了很多东西,但我完全糊涂了。

谢谢。

4

2 回答 2

1

只需在每个数字后显式打印一个空格。

您是否尝试过类似的方法:

for(int r = 0; r < drawing.array.length; r++) {
    for(int c = 0; c < drawing.array[r].length; c++) {
        outFile.print(drawing.array[r][c]);
        outFile.print(" ");
    }
}
于 2012-09-19T18:13:12.123 回答
0
outFile.print(drawing.array[r][c]);
outFile.print(" ");
于 2012-09-19T18:14:19.377 回答