2015-04-08 20 views
2
使用的PrintWriter追加

Board類有一個write方法這樣如何在Java

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類,整理解決後,我想寫startgoal板我output1.txt文件

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

startgoalBoard類的兩個實例。但它並沒有追加,也沒有寫任何東西

我該如何追加?

我想用FileOutputStreamwrite方法的參數,因爲我有很多其他input2.txtinput3.txt相應output2.txtoutput3.txt,所以我沒有使用指定的路徑write方法

請幫我

回答

0

您需要關閉PrintWriter

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

如果我這樣做,在完成'start.write(fo)'後,方法'goal.write (fo)'不會追加 – necroface

+0

比您需要更改代碼的設計,以便您可以重新使用寫入方法。但要寫入文件,您必須關閉資源。 – Masudul

+0

我設法解決這個問題,謝謝:) – necroface