【问题标题】:Reading after appending objects to serialized file in Java [duplicate]将对象附加到Java中的序列化文件后读取[重复]
【发布时间】:2018-04-12 03:44:18
【问题描述】:

我正在用 Java 写入流文件。我已经阅读过,如果我想追加我需要覆盖 WriteStreamHeader。但即使这样做了,我也无法摆脱 StreamCorruptedException。

class AppendingObjectOutputStream extends ObjectOutputStream {
      public AppendingObjectOutputStream(OutputStream out) throws IOException            {
        super(out);
      }
      protected void writeStreamHeader() throws IOException {
        reset();
      }
}
class Student implements Operations,Serializable
{
    private static final long serialVersionUID = 1L;
    private int studid;
    private String sname;
    private int sage;
    private long contact;
    public Student()
    {
        studid = 0;
        sname = null;
        sage = 0;
        contact = 0;
    }
    public void add(Scanner sc)
    {
        System.out.print("Enter student id: ");
        studid = Integer.parseInt(sc.nextLine());
        System.out.print("Enter student name: ");
        sname = sc.nextLine();
        System.out.print("Enter student age: ");
        sage = Integer.parseInt(sc.nextLine());
        System.out.print("Enter student's contact number: ");
        contact=Long.parseLong(sc.nextLine());
    }
    public void show()
    {
        System.out.println("Student's details:");
        System.out.println("Id no: "+studid);
        System.out.println("Name :" + sname);
        System.out.println("Age :" + sage);
        System.out.println("Contact No. :" + contact);
    }
}
class Admin
{
    public void addstu(Scanner sc)
    {
        try
        {
            Student s = new Student();
            s.add(sc);
            boolean b = true;
            FileInputStream fis = null;
            try{
                fis = new FileInputStream("student.ser");
                fis.close();
            }
            catch (FileNotFoundException e)
            {
                b = false;
            }
            FileOutputStream fos = new FileOutputStream("student.ser");
            ObjectOutputStream oos =null;
            if(b == true)
            {   
                System.out.println("Appending objects");
                oos = new AppendingObjectOutputStream(fos);
            }
            else
            {
                System.out.println("Writing objects");
                oos = new ObjectOutputStream(fos);
            }
            oos.writeObject(s);
            oos.close();
            fos.close();
            System.out.println("Student successfully inserted");
            fis = new FileInputStream("student.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student result = (Student) ois.readObject();
            result.show();
            ois.close();
            fis.close(); 
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void displayallstu() 
    {
        try
        {
            FileInputStream fis = new FileInputStream("student.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            System.out.println("Student's List");
            try{
                while(true)
                {
                    Student result = (Student) ois.readObject();
                    result.show();
                }
            }
                 catch (EOFException e) {
                     System.out.println("!!End of file!!");
                    }
            finally{
            ois.close();
            fis.close();}
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
         catch (ClassNotFoundException e) {
                e.printStackTrace();
            } 
    }
}

从main调用,第一次调用addstu()时正常执行,但下次调用时显示Successly Inserted消息,但读取时抛出异常。

java.io.StreamCorruptedException: invalid stream header: 79737200

【问题讨论】:

  • 您没有在附加模式下打开输出流fos。见stackoverflow.com/q/8544771/3690024
  • @Nisha,为什么你删除了这个问题,我正在写答案。
  • @Lucifer 我有另一个命令解决了我的问题,所以!无论如何感谢您的帮助! :)
  • 还不错........

标签: java file serialization


【解决方案1】:

你首先生成的是:

  • 流标头
  • 学生

那么你将覆盖这个:

  • 重置
  • 学生

由于缺少流标头,因此无法读取。您希望文件中包含以下内容:

  • 流标头
  • 学生
  • 重置
  • 学生

为此,您需要在第一次创建文件后以附加模式打开输出流。由于您已经有一个布尔值来指示这是否是必要的,只需将其传递给构造函数:

FileOutputStream fos = new FileOutputStream("student.ser", b);

【讨论】:

  • 如果我这样做,那么我的记录将被附加。但是在我调用 displayallstu() 之后,它会显示第一条记录,然后抛出异常 java.io.StreamCorruptedException: invalid type code: AC
  • 有趣。这以前有效,现在您显然正在走得更远。您的代码可能存在不止一个问题,或者......也许发生了一些变化?您使用的是 Java9 还是更早的版本?明天我会继续看这个;或者也许其他人会在此期间拿起火炬。
  • 衷心感谢!!:) 我再次检查了我的代码,发现根据您的建议,我删除了对 ObjectOutputStream 的更改。现在对 ObjectOutputStream 和 FileOutputStream 都使用附加功能,我可以做到了!!再次感谢!!
猜你喜欢
  • 2018-02-26
  • 2013-03-14
  • 2013-05-10
  • 2018-02-07
  • 2013-06-22
  • 2012-06-03
  • 1970-01-01
  • 2016-01-23
  • 2011-04-11
相关资源
最近更新 更多