Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write and read java serialized objects into a file

I am going to write multiple objects to a file and then retrieve them in another part of my code. My code has no error, but it is not working properly. Could you please help me find what is wrong about my code. I read different codes from different websites, but none of them worked for me!

Here is my code to write my objects to a file: MyClassList is an arraylist which includes objects of my class (which must be written to a file).

for (int cnt = 0; cnt < MyClassList.size(); cnt++) {     FileOutputStream fout = new FileOutputStream("G:\\address.ser", true);     ObjectOutputStream oos = new ObjectOutputStream(fout);     oos.writeObject(MyClassList.get(cnt)); } 

I added "true" to the constructor of the outputstream, because I want to add each object to end of the file. Is that correct?

And here is my code to read the objects from the file:

 try {      streamIn = new FileInputStream("G:\\address.ser");      ObjectInputStream objectinputstream = new ObjectInputStream(streamIn);      MyClass readCase = (MyClass) objectinputstream.readObject();      recordList.add(readCase);      System.out.println(recordList.get(i));  } catch (Exception e) {      e.printStackTrace();  } 

It finally prints out just one object. Now, I don't know if I am not writing correctly or reading correctly!

like image 458
user1419243 Avatar asked Jun 25 '13 09:06

user1419243


People also ask

Can object be written to a file in Java?

Java object Serialization is an API provided by Java Library stack as a means to serialize Java objects. Serialization is a process to convert objects into a writable byte stream. Once converted into a byte-stream, these objects can be written to a file.


2 Answers

Why not serialize the whole list at once?

FileOutputStream fout = new FileOutputStream("G:\\address.ser"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(MyClassList); 

Assuming, of course, that MyClassList is an ArrayList or LinkedList, or another Serializable collection.

In the case of reading it back, in your code you ready only one item, there is no loop to gather all the item written.

like image 191
forty-two Avatar answered Sep 23 '22 12:09

forty-two


As others suggested, you can serialize and deserialize the whole list at once, which is simpler and seems to comply perfectly with what you intend to do.

In that case the serialization code becomes

ObjectOutputStream oos = null; FileOutputStream fout = null; try{     fout = new FileOutputStream("G:\\address.ser", true);     oos = new ObjectOutputStream(fout);     oos.writeObject(myClassList); } catch (Exception ex) {     ex.printStackTrace(); } finally {     if(oos != null){         oos.close();     }  } 

And deserialization becomes (assuming that myClassList is a list and hoping you will use generics):

ObjectInputStream objectinputstream = null; try {     FileInputStream streamIn = new FileInputStream("G:\\address.ser");     objectinputstream = new ObjectInputStream(streamIn);     List<MyClass> readCase = (List<MyClass>) objectinputstream.readObject();     recordList.add(readCase);     System.out.println(recordList.get(i)); } catch (Exception e) {     e.printStackTrace(); } finally {     if(objectinputstream != null){         objectinputstream .close();     }  } 

You can also deserialize several objects from a file, as you intended to:

ObjectInputStream objectinputstream = null; try {     streamIn = new FileInputStream("G:\\address.ser");     objectinputstream = new ObjectInputStream(streamIn);     MyClass readCase = null;     do {         readCase = (MyClass) objectinputstream.readObject();         if(readCase != null){             recordList.add(readCase);         }      } while (readCase != null)             System.out.println(recordList.get(i)); } catch (Exception e) {     e.printStackTrace(); } finally {     if(objectinputstream != null){         objectinputstream .close();     }  } 

Please do not forget to close stream objects in a finally clause (note: it can throw exception).

EDIT

As suggested in the comments, it should be preferable to use try with resources and the code should get quite simpler.

Here is the list serialization :

try(     FileOutputStream fout = new FileOutputStream("G:\\address.ser", true);     ObjectOutputStream oos = new ObjectOutputStream(fout); ){     oos.writeObject(myClassList); } catch (Exception ex) {     ex.printStackTrace(); } 
like image 34
C.Champagne Avatar answered Sep 21 '22 12:09

C.Champagne