为什么只有1个java源文件能够写入同一个文件?

jen*_*fer 1 java file writer

在我的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();}
Run Code Online (Sandbox Code Playgroud)

然后在我的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());

} 
Run Code Online (Sandbox Code Playgroud)

我用System.out.println测试了控制台,输出很好.但是结束文件只显示brokerAcc1.toString(),对于aStockTransaction.toString()没有任何内容.为什么?怎么解决?提前致谢!

Oli*_*rth 9

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