How to write and read java serialized objects into a file

165,659

Solution 1

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.

Solution 2

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();
}

Solution 3

Simple program to write objects to file and read objects from file.

package program;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TempList {

  public static void main(String[] args) throws Exception {
    Counter counter = new Counter(10);

    File f = new File("MyFile.txt");
    FileOutputStream fos = new FileOutputStream(f);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(counter);
    oos.close();

    FileInputStream fis = new FileInputStream(f);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Counter newCounter = (Counter) ois.readObject();
    System.out.println(newCounter.count);
    ois.close();
  }

}

class Counter implements Serializable {

  private static final long serialVersionUID = -628789568975888036 L;

  int count;

  Counter(int count) {
    this.count = count;
  }
}

After running the program the output in your console window will be 10 and you can find the file inside Test folder by clicking on the icon show in below image.

enter image description here

Share:
165,659
user1419243
Author by

user1419243

Updated on July 05, 2022

Comments

  • user1419243
    user1419243 almost 2 years

    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!

  • wea
    wea over 10 years
    Don't forget to close fout and oos!! [fout.close(); oos.close();]
  • Augustin
    Augustin over 9 years
    Performance of this can be vastly improved by using the BufferedOutputStream between the FileOutputStream and the ObjectOutputStream.
  • starscream_disco_party
    starscream_disco_party over 8 years
    Should have been the accepted answer since you implemented try/catch
  • User
    User over 7 years
    The question is about reading and writing, but this answer only covers writing.
  • zurbergram
    zurbergram over 6 years
    If you using Java 8 you can use try with resources
  • C.Champagne
    C.Champagne over 6 years
    @zurbergram You are right and it is preferable. I just didn't have the opportunity to use it at the time I wrote this answer