James
James

Reputation: 53

Issue with Deserialization

I have a method in my program that reads from a file, and I have associated with both a FileInputStream variable and an ObjectInputStream variable. However, I do not know how many objects will be serialized in the file when I run the program, so I do not know how many objects to deserialize using the method readObject(). This is the method from my project:

public void importResults(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
    TestEntry entry = null;
    try(FileInputStream fis = new FileInputStream(file)) {
        ObjectInputStream ois = new ObjectInputStream(fis);

        tests.clear();

        entry = (TestEntry)ois.readObject();

        tests.add(entry);

        ois.close();
    }
}

The variable entry is where I will store TestEntry objects that I deserialize from the file. The problem is, if I try to deserialize too many objects, I get an EOFException. How do I get my program to figure out how many objects there are serialized in the file so I can deserialized the right amount? Any help will be greatly appreciated! Thank you.

Upvotes: 1

Views: 145

Answers (4)

user207421
user207421

Reputation: 310860

Just read in a loop until you get EOFException, which is thrown when there are no more objects to read.

Upvotes: 1

chenchuk
chenchuk

Reputation: 5742

You can add the object to a List and serialize/deserialize that list.

    // entry = (TestEntry)ois.readObject();
    List<TestEntry> entries = (List<TestEntry>)ois.readObject();

Upvotes: 0

KeV
KeV

Reputation: 2891

Maybe you could write the number of objects that are present in the file and for the deserialization first read that number (however I am not sure if one file can contain multiple serialized objects).

However if you put your objects in an ArrayList that you serialize, you can write that ArrayList to the file and deserialize it just by reading one object.

I made a test class A and this works :

public class A implements Serializable {
    int a, b, c;
} 

public static ArrayList<A> deserializeObjects(File file) throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fIn = new FileInputStream(file);
    ObjectInputStream oIn = new ObjectInputStream (fIn);
    ArrayList<A> objects = null;

    // Read array of objects
    if(fIn.available() > 0) {
        objects = (ArrayList<A>) oIn.readObject();
    }

    oIn.close();
    fIn.close();

    return objects;
}

public static void serializeObjects(File file, ArrayList<A> objects) throws IOException {
    FileOutputStream fOut = new FileOutputStream(file);
    ObjectOutputStream oOut = new ObjectOutputStream(fOut);

    // Write the whole arraylist to file
    oOut.writeObject(objects);
    oOut.flush();
    oOut.close();
    fOut.close();
}

public static void main(String args[]) throws IOException, ClassNotFoundException {
    // Make test objects
    A o1 = new A();
    A o2 = new A();

    o1.a = 1; o1.b = 2; o1.c = 3;
    o2.a = 4; o2.b = 5; o2.c = 6;

    ArrayList<A> objects = new ArrayList<A>();
    objects.add(o1);
    objects.add(o2);

    File f = new File("yourFile");
    f.createNewFile();

    serializeObjects(f, objects); // Serialize arraylist
    ArrayList<A> deserialized = deserializeObjects(f); // Deserialize it

    // Success??
    System.out.println("a : " + deserialized.get(0).a + ", b : " + deserialized.get(0).b + ", c : " + deserialized.get(0).c);
    System.out.println("a : " + deserialized.get(1).a + ", b : " + deserialized.get(1).b + ", c : " + deserialized.get(1).c);
}

Upvotes: 1

exception_catcher
exception_catcher

Reputation: 46

I don't think there is a way to count how many Objects are serialized since one file can only hold one Object. You are probably getting EOFException because you are doing something like below. Since there is only one Object in a file no matter how many you serialize, reading twice from the same Stream will result in EOFException.

in.readObject();
in.readObject();

I just tested and you can read an Object any number of times from the same file as long as you don't do the above or something similar.

My test is below

public static void main(String []args) {
     writeObject(new Integer(333));
     for(int i = 0; i < 9999; i++) {
         Integer i = (Integer)readObject();
         System.out.println(i);//prints 333
     }
}
public static void writeObject(Object obj) {
    try {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("file.dat"));
        out.writeObject(obj);
        out.close();
    }
    catch(IOException e) {}
}
public static Object readObject() {
    Object obj = null;
    try {
    ObjectInputStream in = new ObjectInputStream(new FileInputStream("file.dat"));

        obj = in.readObject();
        in.close();
    }
    catch(IOException e){}
    catch(ClassNotFoundException e){}
    return obj;
}

Upvotes: 1

Related Questions