In my StockTransaction.java, this runs first
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();}
Then in my brokerageAccount.java,this runs last
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());
}
I tested with System.out.println to console, the output is fine.
But The end file only show the brokerAcc1.toString(), nothing for aStockTransaction.toString().
Why? How to solve it? Thanks in advance!
You need to use
FileOutputStream(filename, true)in order to append to an existing file.