java - Deserializing an ArrayList -


i having trouble deserializing object file in java in new session.

i can serialize arraylist file, , deserialize in same session, however, if create new main object , try deserialize same file worked in previous session, code not work.

why be?

here code:

public void loadpersonlistfromfile(string filename) {   try   {      personlist = new arraylist<person>();                objectinputstream in = new objectinputstream(new fileinputstream(filename));      arraylist<person> personlist = (arraylist<person>) in.readobject();      in.close();   }   catch (exception e)   {      e.printstacktrace();   } }  public void savepersonfiletofile(string filename) {   try   {      objectoutputstream out = new objectoutputstream(new fileoutputstream(filename));      out.writeobject(personlist);      out.close();   }   catch (exception e)   {      e.printstacktrace();   } }  

edit

the deserialization seems working, because if .size() call on list in loadpersonlistfromfile method, value of 2. however, if call .size() method on list outside of loadpersonlistfromfile method, .size() shown 0. why be? need copy deserialized arraylist private field?

i have following private field store arraylist @ top of class:

private arraylist<person> personlist; 

edit2

got working. here code:

public void loadpersonlistfromfile(string filename) {   try   {      objectinputstream in = new objectinputstream(new fileinputstream(filename));      arraylist<person> personlistfromfile = (arraylist<person>) in.readobject();      in.close();      personlist = personlistfromfile;            }   catch (exception e)   {      e.printstacktrace();   } } 

it seems didn't define static final long serialversionuid in person class. basically, when compile serializable class has no serialversionuid defined in source code, java compiler define random value.

to solve issue, define following in person class:

private static final long serialversionuid = 1l; 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -