The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
November 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

How to reuse object in serialization ?

Posted by Tony on November 22, 2001 at 8:00 AM

Hi there,

My question is about objects reusing in serialization.

Suppose such a code :

HashMap hm = new HashMap();
try{
FileOutputStream ostream = new FileOutputStream("object.ser");
ObjectOutputStream p = new ObjectOutputStream(ostream);

for (int i=0;i<10;i++){
hm.clear();
// hm = new HashMap();
hm.put("int",new Integer(i));
ll.addLast(hm);
p.writeObject(hm);
p.flush();
} // for
ostream.close();
} catch (IOException e){
System.out.println(e);
} //try/catch


System.out.println("Content of serialized file");
try {
FileInputStream istream = new FileInputStream("object.ser");
ObjectInputStream s = new ObjectInputStream(istream);
for (int i=0;i<10;i++){
System.out.println("Object["+i+"] = "+(HashMap) s.readObject()) ;
} // for
istream.close();
} catch (Exception e){
System.out.println(e);
} //try/catch

So if i use hashmap clearing and filling then flushing. Then i deserialize it and I get printed list where every Hashmap is the same, as first one (in this case all objects has the same entry "int"=0) If create new object (in code commented out) then everything's fine. How can I reuse object ? Why it doesn't work ?

Thanx

Tony



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us