Java serialization - java.lang.ClassCastException Error -


i have storageimpl class serializable. class has inner class record.

public class storageimpl <id, t extends idatastore<id>> implements istorage <t, id> {      private class record implements serializable     {         private static final long serialversionuid = 1l;          private long key;         private list<t> datalist;          private record()         {             key      = new long(0);             datalist = new arraylist<t>();         }     } } 

i want store , retrieve record in turn has

private long key; private list<t> datalist; 

once retrieve record, can books in booklist member of record.

public class storageimpl <id, t extends idatastore<id>> implements istorage <t, id> {     private class record implements serializable     {         private static final long serialversionuid = 1l;          private long key;         private list<t> datalist;          private record()         {             key      = new long(0);             datalist = new arraylist<t>();         }      private string datafile;     private record record;      public storageimpl(string datafile)     {         this.datafile = datafile;         record        = new record();     }       public list<t> retrieve() throws exception     {         list<t> datalist = new arraylist<>();          file file = new file(datafile);          if (!file.exists())         {             return datalist;         }                  try (fileinputstream fis = new fileinputstream(datafile); objectinputstream ois = new objectinputstream(fis))         {             record = (record) ois.readobject();         }         catch (ioexception e)         {             e.printstacktrace();         }         catch (classnotfoundexception e)         {             e.printstacktrace();         }          (int = 0; < record.datalist.size(); i++)          {             datalist.add(record.datalist.get(i));         }          return datalist;     }     @suppresswarnings("unchecked")     @override     public void store(t t) throws exception     {        list<t> datalist = retrieve();         ++record.key;                t.setid((id) record.key);        datalist.add(t);         record.datalist = datalist;                writedata(record);     }     private void writedata(record record)     {         try (fileoutputstream fos = new fileoutputstream(datafile);              objectoutputstream oos = new objectoutputstream(fos))         {             oos.writeobject(record);         }         catch (ioexception e)         {             e.printstacktrace();         }     } } 

update:

please note want persist record , not list.

the error after code changes looks relates record not being serializable:

caused by: java.io.notserializableexception:

atjava.io.objectoutputstream.writeobject0(objectoutputstream.java:1178) @ java.io.objectoutputstream.writeobject(objectoutputstream.java:348) @ storageimpl.writedata(storageimpl..java:159) 

and call inside writedata() @ line 159

oos.writeobject(record); 

it looks you're reading whole list file, casting single record.

to load arraylist containing record objects need record serializable, you're still loading entire arraylist, cast incorrect (i.e. can store result directly recordlist).

list<record> recordlist = new  arraylist<>();  try (fileinputstream fis = new fileinputstream(datafile); objectinputstream ois = new objectinputstream(fis)) {     recordlist = (list<record>) ois.readobject(); } catch (ioexception e) {     e.printstacktrace(); } catch (classnotfoundexception e) {     e.printstacktrace(); } 

Comments