I want to serialize a Hashtable to a file using ObjectOutputStream.writeObject()
, but I want the writer to always overwrite any existing object such that only a single object exists:
FileOutputStream fout = new FileOutputStream( f);
ObjectOutputStream writer = new ObjectOutputStream(fout);
writer.writeObject( hashtable);
writer.flush();
The hastable is updated intermittently at runtime, so I use a helper method to persist its state:
private void persistObject( Hashtable ht){
writer.writeObject( ht);
writer.flush();
}
The problem is every time I call writeObject()
, a new hastable is appended to the file; Is there any way to just overwrite whatever is in the file so only a single object is ever persisted?