2011-04-06 50 views
1

在我StockTransaction.java,這首先運行爲什麼只有1個java源文件能夠寫入同一個文件?

try{ 
     FileOutputStream fos = new FileOutputStream("C:"+File.separatorChar+"transactions.dat"); 
     OutputStreamWriter osw = new OutputStreamWriter(fos); 
     BufferedWriter writer = new BufferedWriter(osw); 
     writer.append(aStockTransaction.toString()); 
     writer.append("******This Transaction ends Here.*****"); 
     writer.flush(); 
     writer.close();} 

然後在我的brokerageAccount.java,該跑最後一棒

try { 
        FileOutputStream fos = new FileOutputStream("C:"+File.separatorChar+"transactions.dat"); 

     OutputStreamWriter osw = new OutputStreamWriter(fos); 
     BufferedWriter writer = new BufferedWriter(osw); 
     writer.append(brokerageAcc1.toString()); 
     writer.append("******This is end of File*****"); 
     writer.flush(); 
     writer.close(); 
     //System.out.println(brokerageAcc1.toString()); 

} 

我的System.out.println測試安慰,輸出精。 但最終的文件只顯示brokerAcc1.toString(),沒有爲aStockTransaction.toString()。 爲什麼?如何解決它?提前致謝!

回答

9

您需要使用FileOutputStream(filename, true)以附加到現有的文件。

相關問題