Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Here I am creating a HashMap, adding it to a list, then serializing this list. Then closing the ObjectOutputStream and FileOutputStream. Then reopening the ObjectOutputStream and FileOutputStream and writing second list to it . While reading the file i am getting this error:

Exception in thread "main" java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
    at javaapplication27.NewMain4.main(NewMain4.java:60)
Java Result: 1

My code is this:

    HashMap<String, String> hm1 = new HashMap<String, String>(); 
    hm1.put("1", "A");
    hm1.put("2", "B");
    hm1.put("3", "C");

    ArrayList al1 = new ArrayList();
    al1.add(hm1); 

    HashMap<String, String> hm2 = new HashMap<String, String>();
    hm2.put("4", "A1");
    hm2.put("5", "B1");
    hm2.put("6", "C1");

    ArrayList al2 = new ArrayList();
    al2.add(hm2);

    FileOutputStream fos = new FileOutputStream("D:/sample.ser", true);
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(al1);
    oos.close(); // closing the sreams
    fos.close();

    fos = new FileOutputStream("D:/sample.ser", true);
    oos = new ObjectOutputStream(fos);
    oos.writeObject(al2);


    oos.close();
    fos.close();


    FileInputStream fis = new FileInputStream("D:/sample.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    ArrayList al = (ArrayList) ois.readObject();
    System.out.println("First: " + al);

    al = (ArrayList) ois.readObject(); // This is line 60 in my code
    System.out.println("Second: " + al);
  1. What is the reason for the error?
  2. What to do if I want to write two or more objects to a serialized file and read it back?
  3. What to do if I have to serialize the objects to the same file e.g. sample.ser from different Java classes, and read all the objects back?
share|improve this question
Per out FAQ, this question is off-topic here. It is not acceptable to ask questions where they do not belong because you have been blocked from asking new questions on a Stack Exchange site. – Thomas Owens Feb 6 at 11:11
Seems like a reasonable StackOverflow question. But briefly, the problem is that the OP is writing two object streams into the same file. An object stream contains headers along with the serialized objects. To read the file successfully, open a second ObjectInputStream just before line 60 and read the second object from that. You can't close the first ObjectInputStream, since that will close the underlying FileInputStream and you'll lose your place in the file. – Stuart Marks Feb 7 at 1:57

closed as off topic by Joachim Sauer, Thomas Owens Feb 6 at 11:10

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

1 Answer

StreamCorruptedException is thrown when control information that was read from an object stream violates internal consistency checks.

I tested your code by commenting 2nd write and 2nd read; and it works fine. Also there is no problem in opening a closed stream again and then writing another object. The problem is occurring only when you try to read 2nd time. This is also evident from the StreamCorruptedException description.

Ideally its a good practice to make write/read calls as atomic in Serialization.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.