0

I am trying to do a program that can read all the objects that I put into the file. The input works well and I put 8 objects in, But when I try to read them from the file,It shows the java.io.StreamCorruptedException, It seems it can not display the all methods I entered. I don't know where I do wrong. Is there any other way to realize the function that display every objects in the system. Thanks And I search the solution Appending to an ObjectOutputStream but I can't understand the answer,I will very appreciate it if someone can explain it

import java.util.*;
import java.io.*;
public class driver
{
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        ObjectOutputStream objOut = null;
        ObjectInputStream objIn = null;
        Pet p = null;
        Person owner = null;

        do{
            System.out.println("what u want to do");
            System.out.println("1.add the pet\n2read the pet\n3get the weight\n4 system exit");
            int option = keyboard.nextInt();
            keyboard.nextLine();

            switch(option)
            {
            case 1:
                try 
                {
                    objOut = new ObjectOutputStream(new FileOutputStream("pet.dat",true));
                    owner = new Person();
                    owner.getInput();
                    System.out.println("Type of pet?(1-Mammal;2-Fish;3-Amphibian");
                    int type = keyboard.nextInt();
                    keyboard.nextLine();
                    switch (type)
                    {
                    case 1:
                        p = new Mammal();
                        p.owner = owner;
                        p.getInput();
                        objOut.writeObject(p);
                        objOut.close();
                        break;
                    case 2:
                        p = new Fish();
                        p.owner = owner;
                        p.getInput();
                        objOut.writeObject(p);
                        objOut.close();
                        break;
                    case 3:
                        p = new Amphibian();
                        p.owner = owner;
                        p.getInput();
                        objOut.writeObject(p);
                        objOut.close();
                        break;
                    }
                } 

                catch (Exception e) 
                {
                    e.printStackTrace();
                    System.out.println("here");
                }

                break;

            case 2:
                try 
                {
                    objIn = new ObjectInputStream(new FileInputStream("pet.dat"));

                    try 
                    {
                        while(true)
                        {
                            p = (Pet)objIn.readObject();
                            System.out.println(p);
                        }

                    } 
                    catch (EOFException e) 
                    {
                        System.out.println("System ends");
                    }
                } 

                catch (Exception e)
                {
                    e.printStackTrace();

                    System.out.println("something in driver");
                }

                break;

            case 3:
                try 
                {
                    objIn = new ObjectInputStream(new FileInputStream("pet.dat"));

                    try 
                    {
                        while(true)
                        {
                            p = (Pet)objIn.readObject();
                            System.out.println(p.getname() + "'s weight is: " + p.getweight());
                        }

                    } 
                    catch (EOFException e) 
                    {
                        System.out.println("System ends");
                    }
                } 

                catch (Exception e)
                {
                    e.printStackTrace();
                    System.out.println("something in driver");
                }
                break;

            case 4:

                System.exit(0);
            }
        }while(true);
    }
}
Community
  • 1
  • 1
xiaopassion
  • 73
  • 1
  • 9

1 Answers1

0
objOut = new ObjectOutputStream(new FileOutputStream("pet.dat",true));

You can't append to object stream files, at least not without special measures. You get 'StreamCorruptedException: invalid type code AC', which is indeed both an exception and an error, contrary to your apparent belief as expressed in your comment. Duplicate of hundreds of questions here on that topic. See here for a full explanation.

Community
  • 1
  • 1
user207421
  • 305,947
  • 44
  • 307
  • 483