简体   繁体   English

写入所有文件后如何读取文件中的所有对象

[英]How to read the all objects in file after I write all the files

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. 输入效果很好,我放入了8个对象,但是当我尝试从文件中读取它们时,它显示了java.io.StreamCorruptedException,似乎无法显示我输入的所有方法。 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 谢谢并且我搜索了解决方案追加到ObjectOutputStream上,但是我听不懂答案,如果有人可以解释我将不胜感激

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);
    }
}
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. 您会收到“ StreamCorruptedException:无效类型代码AC”,这实际上既是异常,也是错误,与您在注释中表达的明显信念相反。 Duplicate of hundreds of questions here on that topic. 与该主题相关的数百个问题重复出现。 See here for a full explanation. 完整说明请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM