I'm trying to create a system where users can save a series of Message objects to a .txt file, which can then be de-serialized and read by the recipient of the Message. However each time I try and write to the file I simply overwrite the object that was there previously.
My current code is as follows:
public class Comms{
Message msg;
public void sendMessage(Message myMessage){
msg = myMessage;
//Generate Message ID
String msgID = "MSG" + genMsgID();
myMessage.setMessageID(msgID);
//********************************************************
try
{
FileOutputStream logFile = new FileOutputStream("logFile.txt");
NoHeaderObjectOutputStream out = new NoHeaderObjectOutputStream(logFile);
out.writeObject(myMessage);
out.close();
logFile.close();
}catch(IOException i)
{
}
}
}
class NoHeaderObjectOutputStream extends ObjectOutputStream {
public NoHeaderObjectOutputStream(OutputStream os) throws IOException {
super(os);
}
protected void writeStreamHeader() {}
}
I appreciate that there are similar questions out there but with my limited knowledge I'm struggling to make sense of the answers provided.
If anyone has a solution to this or some pointers that'd be greatly appreciated.