The serialization and deserialization of the object works fine without private void writeObject ( ObjectOutputStream os ) / read object methods. However when I add them , i get a run time error as
Exception in thread "main" java.io.IOException: No such file or directory at java.io.FileOutputStream.writeBytes(Native Method) at java.io.FileOutputStream.write(FileOutputStream.java:260) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1682) at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1591) at java.io.ObjectOutputStream.writeNonProxyDesc(ObjectOutputStream.java:1173) at java.io.ObjectOutputStream.writeClassDesc(ObjectOutputStream.java:1127) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079) at java.io.ObjectOutputStream.writeFatalException(ObjectOutputStream.java:1398) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:305) at Chapter6.TestSerialize.main(TestSerialize.java:23)
While usually it is a good idea to close all your streams, you should do it at the right times. Usually, that is not in a method of which the stream is a parameter - you may want to write to the stream after the method has finished.
Safest is to close it in the same method as where it was opened, if possible. Not in methods called from that method.
And one final note: don't use .txt as your file extension. That suggest it is a text file which it definitely isn't. No extension at all, or .bin are more appropriate. [ September 29, 2008: Message edited by: Rob Prime ]