如何在Java中使用PrintWriter追加

时间:2015-04-08 04:30:25

标签: java file append fileoutputstream printwriter

我的Board班级有write这样的方法

public void write(FileOutputStream fo) throws IOException
    {
        PrintWriter out=new PrintWriter(fo,true);
        for (int i = 0; i < size; i++) {
            String formatted="%3d";
            for (int j=0;j<size;j++)
            {
                out.append(String.format(formatted,arr[i][j]));
            }
            out.append(System.getProperty("line.separator"));
        }
        out.append(System.getProperty("line.separator"));
    }

我在BFSSolution课程中使用此主板解决Unblock Me游戏,在完成解决后,我想将startgoal板写入我的output1.txt文件

fo=new FileOutputStream("output\\output1.txt");
start.write(fo);
goal.write(fo);

startgoalBoard类的两个实例。但它根本没有附加,也没有写任何东西

我如何追加?

我想使用FileOutputStream作为write方法的参数,因为我还有许多其他input2.txt input3.txtoutput2.txt output3.txt },所以我没有在write方法

中使用指定的路径

请帮帮我

1 个答案:

答案 0 :(得分:0)

您需要关闭PrintWriter

 public void write(FileOutputStream fo) throws IOException{
    PrintWriter out=new PrintWriter(fo,true);
    .....
    out.close();// To save need to close PrintWriter.
}
相关问题