1

在我的 StockTransaction.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()。为什么?如何解决?提前致谢!

4

1 回答 1

9

您需要使用FileOutputStream(filename, true)才能附加到现有文件。

于 2011-04-06T19:24:53.390 回答