java - Syntax of Serializing a LinkedList<Object> -


as exercise, creating list of books linkedlist , using comparator interface sort them author or title. first, create class of books , ensure print screen way want to:

class book {     string title;     string author;     public book(string t, string a){         title = t;         author = a;     }     public string tostring(){         return title + "\t" + author;     } } 

next, created linkedlist takes object book:

linkedlist<book> booklist = new linkedlist<>(); 

a class implements comparator created sorts according title/author , displays them on jtextarea inside of main frame. of working fine, there's 1 glaring bug...i cannot save file!

i've tried class implements serializable takes linkedlist argument , writes .obj file. when load it, fails. creates file, notserializableexception. tried saving file .ser told make easier save it, failed on loading, well.

does know method serializing linkedlist using bufferedreader? or there approach this? thank in advance time reading through question, , thank advice, comments, or answers can provide. thanks, guys.

addition: entire code:

import javax.swing.*; import java.util.*; import java.awt.event.*; import java.awt.*; import java.io.*;  public class check {      bookcompare bc = new bookcompare();     linkedlist<book> booklist = new linkedlist<>();     jframe frame;     jpanel northpanel, centerpanel, southpanel;     jbutton addbook, savebook, loadbook;     jtextfield authorfield, titlefield;     jtextarea displaybook;      public static void main(string[] args) {         new check().buildgui();     }      private void buildgui() {         frame = new jframe("book list");         frame.setdefaultcloseoperation(jframe.exit_on_close);          northpanel = new jpanel();         centerpanel = new jpanel();         southpanel = new jpanel();          addbook = new jbutton("add book");         addbook.addactionlistener(new addbutton());         savebook = new jbutton("save list");         savebook.addactionlistener(new savebutton());         loadbook = new jbutton("load list");         loadbook.addactionlistener(new loadbutton());          jlabel authorl = new jlabel("author:");         authorfield = new jtextfield(10);         jlabel titlel = new jlabel("title:");         titlefield = new jtextfield(10);          displaybook = new jtextarea(20,40);         displaybook.seteditable(false);         jscrollpane scroll = new jscrollpane(displaybook);         scroll.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always);         scroll.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_never);          northpanel.add(titlel);         northpanel.add(titlefield);         northpanel.add(authorl);         northpanel.add(authorfield);         centerpanel.add(scroll);         southpanel.add(addbook);         southpanel.add(savebook);         southpanel.add(loadbook);          frame.getcontentpane().add(borderlayout.north,northpanel);         frame.getcontentpane().add(borderlayout.center,centerpanel);         frame.getcontentpane().add(borderlayout.south,southpanel);          frame.setvisible(true);         frame.setsize(500, 500);         frame.setresizable(false);         frame.setlocation(375, 50);     }      class addbutton implements actionlistener {         public void actionperformed(actionevent e) {             addtolist();             sortanddisplay();             readynext();         }     }      private void addtolist() {         string newtitle = titlefield.gettext();         string newauthor = authorfield.gettext();         booklist.add(new book(newtitle, newauthor));     }      private void sortanddisplay() {         displaybook.settext(null);         collections.sort(booklist,bc);         for(int = 0; < booklist.size(); i++){             displaybook.append(booklist.get(i).tostring());         }     }      private void readynext() {         authorfield.settext(null);         titlefield.settext(null);         titlefield.requestfocus();     }      class savebutton implements actionlistener {         public void actionperformed(actionevent e) {             try {                 objectoutputstream oo = new objectoutputstream(new fileoutputstream("save.ser"));                 oo.writeobject(booklist);                 oo.close();             } catch (ioexception ioe){}         }     }      class loadbutton implements actionlistener {         public void actionperformed(actionevent e) {             try {                 objectinputstream oi = new objectinputstream(new fileinputstream("save.ser"));                 object booksin = oi.readobject();                 book inbook = (book)booksin;                 booklist.add(inbook);                 sortanddisplay();                 oi.close();             } catch (exception exc){}         }     }      class bookcompare implements comparator<book> {         public int compare(book one, book two) {             return one.title.compareto(two.title);         }     }      class book implements serializable{         string title;         string author;         public book(string t, string a) {             title = t;             author = a;         }         public string tostring(){             return title + "\t" + author + "\n";         }     } } 

make book class serializable

class book implements serializable{   string title;   string author;   public book(string t, string a){      title = t;      author = a;   }   public string tostring(){      return title + "\t" + author;   } } 

the serialization interface has no methods or fields , serves identify semantics of being serializable.so don't need implement method declaring enough.
according java docs

serializability of class enabled class implementing java.io.serializable interface. classes not implement interface not have of state serialized or deserialized. subtypes of serializable class serializable. serialization interface has no methods or fields , serves identify semantics of being serializable.

update resolve current issue
have implemented de-serialization process wrongly.this correct implementation.try desired result.

class loadbutton implements actionlistener {     public void actionperformed(actionevent e) {         try {             objectinputstream oi = new objectinputstream(new fileinputstream("save.ser"));             object booksin = oi.readobject();             booklist = (linkedlist<book>)booksin;             sortanddisplay();             oi.close();         } catch (exception exc){}     }  } 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -