1

I wrote 2 objects to a file but when I try to read back the objects written it throws an exception in the desrialize method. It works fine when I write a single object but fails with 2 objects written to the file and the exception is thrown while reading the second object.

File_obj.Serialize_object(d, "/home/dasman/doc/serobj1.bin");
File_obj.Serialize_object(b, "/home/dasman/doc/serobj1.bin");
File_obj.deSerialize_object("/home/dasman/doc/serobj1.bin");



java.io.StreamCorruptedException: invalid type code: AC
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1373)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:368)
at Testpackage.File_obj.deSerialize_object(File_obj.java:242)
at File_test.main(File_test.java:28)

The method for serialize and deserialize in File_obj class are here

public static void Serialize_object(Object obj,String filename){
    File a = new File(filename);
    ObjectOutputStream out = null;
    try{
     out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(a,true)));
    }catch(IOException e){
        e.printStackTrace();
    }
    try{

    out.writeObject(obj);
    out.close();
    }catch(IOException e){
        e.printStackTrace();
    }

}
    public static void deSerialize_object(String filename){
    File a = new File(filename);
    int objcount = 0;
    ObjectInputStream in = null;
    try{
     in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));
    }catch(IOException e){
        e.printStackTrace();
    }
    Serializable_test obj1 = null;
    try{
    while(true){    

    obj1 = (Serializable_test) in.readObject();
    objcount++;
    System.out.println("The object area is :" + obj1.get_area());

    }
    }catch(EOFException e){
        System.out.println("END of object files reached objects read :" + objcount);
        //in.close();
    }catch(IOException e){
        e.printStackTrace();
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }


}
1
  • Despite making a mistake over the overwrite, Bohemian offered some good tips on how to solve this. If those suggestions do not work for you, I suggest you post an SSCCE. Commented Jun 4, 2011 at 22:49

3 Answers 3

1

You can't append to a file that was created via ObjectOutputStream. You have to rewrite the whole file. ObjectOutputStream writes a header that ObjectInputStream won't understand if it reoccurs inside the file.

You can however write multiple objects to the same ObjectOutputStream, and read them back, contrary to what is stated in other answers here.

Sign up to request clarification or add additional context in comments.

Comments

1

The deserialization routine can only only re-constitute one object (or object graph) at a time. It assumes that the serialized content being read in only pertains to a single object and treats the file containing data from two objects as corrupted..

3 Comments

hmm so what if i write an array of objects in one go without closing the channel? can it reconstitute all the objects then?.
Yes. The entire object graph would be serialized. That was Bohemian's 'B' suggestion.
-1. Completely incorrect. You can write as many objects as you like. But you have to to write them all with the same ObjectOutputStream. See my answer.
1

What's your question here? You are writing two objects into the same file - of course the second one will be appended to the first - java can't handle this!

To fix, either:

  • A) Write to different files, or
  • B) Put the objects in an array or Collection (eg List) and write that to the one file and deserialize the array/Collection and retrieve your objects from there

3 Comments

But if you notice I have FileOutputStream(a,true)) so it should append to the file right?
"You are writing two objects into the same file - of course the second one will overwrite the first." Nu-uh - Not when the FileOutputStream(a,true) ..is set to append.
Incorrect. Java can indeed handle this, as long as it is done within the same ObjectInputStream. Otherwise you get this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.