在Java中将对象附加到序列化文件后进行读取

时间:2017-10-31 03:37:39

标签: java file serialization

我正在用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()时正确执行,但在下次调用时,显示Successfully Inserted消息,但在读取时抛出异常。

java.io.StreamCorruptedException: invalid stream header: 79737200

1 个答案:

答案 0 :(得分:1)

您首先生成的是:

  • 流标题
  • 学生

然后你用覆盖

  • 重置
  • 学生

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

  • 流标题
  • 学生
  • 重置
  • 学生

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

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