file io - help for solving a problem with java I/O -


i have created class named animal implements serializable. each object of animal class has name , type. in main method of program, create object of type animal , assign name , type it. want write object file , after read read file animal object. aim have created following mthods:

    animal anim=new animal();     animal anim2 = new animal();     //writing file     private void writeonfile() {   try{     fileoutputstream fout = new fileoutputstream("c:\\animal.ser");    objectoutputstream oos = new objectoutputstream(fout);       oos.writeobject(anim);    oos.close();    system.out.println("done");       }catch(exception ex){       ex.printstacktrace();      }   }     // reading file     private animal readfromfile() {    try{       fileinputstream fin = new fileinputstream("c:\\animal.ser");       objectinputstream ois = new objectinputstream(fin);       anim = (animal) ois.readobject();       ois.close();        return anim;       }catch(exception ex){       ex.printstacktrace();       return null;      }   } 

but want create linkedlist , add several objects of animal linkedlist. how can change writeonfile() , readfromfile() methods write work? please guide me?

karianna's reply good, using foreach. homework apparently focuses on features of serializable interface, let's use it.

since animal class implements serializable linkedlist class can pass linkedlist objectoutputstream , load in similar manner.

linkedlist<animal> list = new linkedlist<animal>(); (... add elements ...) objectoutputstream oos = new objectoutputstream(fout);    oos.writeobject(list); 

the loading part, want learn exercise, don't ;)


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -