Java - 序列化 - EOFException问题 [英] Java - Serialization - EOFException Issue

查看:190
本文介绍了Java - 序列化 - EOFException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于今天关于序列化的第二篇文章感到抱歉,修复一个问题导致另一个问题。

Sorry for the second post today regarding serialization, fixing one issue caused another.

作为州的状态 - Java - 序列化 - NotSerializableException问题 - 我有一个包含以下类的项目

As states here - Java - Serialization - NotSerializableException Issue - I have a project with following classes


Student.java

Student.java

StudentsCollection.java

StudentsCollection.java

学生创建我的学生对象(自我解释),我的StudentsCollection()实例化存储我的学生对象的学生类型列表,当我尝试保存/加载我使用此代码的对象时获取以下内容抛出异常:

Students creates my Student object(Self explanatory) and my StudentsCollection() instantiates a list of type Student which stores my Student objects, when trying to save/load the objects I use this code and get the following exception thrown:

       /**
         * Open student collection
         */
       public void openCollection(){
          try {
             FileInputStream e = new FileInputStream("students.ser");
             ObjectInputStream inputSteam = new ObjectInputStream(e);
              while(inputSteam.readObject() != null){
                  this.list.add((Students)inputSteam.readObject());
              }
          } catch (FileNotFoundException var3) {
              var3.printStackTrace();
             JOptionPane.showMessageDialog(null, "File not found");
          } catch (IOException var4) {
              var4.printStackTrace();
             JOptionPane.showMessageDialog(null, "IO Exception");
          } catch (ClassNotFoundException var5) {
              var5.printStackTrace();
             JOptionPane.showMessageDialog(null, "Required class not found");
          }

       }

投掷:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:558)
    at jdatabase.main.MainController.main(MainController.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我只有一个Student对象添加到我的列表中,保存列表并重新打开后,我要求控制台打印出该列表,学生实际打印出来。但是,当我创建多个学生对象并每次将其ID增加1并添加它们时,控制台会按顺序打印它们然后重新打印(出于某些奇怪的原因)并最终跳过一些。

I only have one Student object added to my list, after saving the list and re-opening it, I ask the console to print out that list, the Student actually prints out. However when I created multiple student objects and incremented their ID by 1 each time and added them, the console would print them ALL in order then re-print(for some odd reason) and end up skipping a few.

如果您需要更多代码,请询问。 saveCollection()现在运行正常

If you need any more code just ask. The saveCollection() is working fine now

修改后的代码:
/ **

Revised code: /**

     * Open student collection
     */
   public void openCollection(){
      try {
         FileInputStream e = new FileInputStream("students.ser");
         ObjectInputStream inputSteam = new ObjectInputStream(e);
          while((obj = inputSteam.readObject()) != null){
              this.list.add((Students)obj);
          }
      } catch (FileNotFoundException var3) {
          var3.printStackTrace();
         JOptionPane.showMessageDialog(null, "File not found");
      } catch (IOException var4) {
          var4.printStackTrace();
         JOptionPane.showMessageDialog(null, "IO Exception");
      } catch (ClassNotFoundException var5) {
          var5.printStackTrace();
         JOptionPane.showMessageDialog(null, "Required class not found");
      }

   }

抛出:

java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2598)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1318)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370)
    at jdatabase.objects.students.StudentsCollection.openCollection(StudentsCollection.java:559)
    at jdatabase.main.MainController.main(MainController.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

只有1个学生对象,并在控制台中打印以下内容:

Only have 1 student object and the following is printed in the console:


学生姓名:学生
学生姓氏:默认
学生证:0学生
DoB:1/1/90

Student name: Student Student surname: Default Student ID: 0 Student DoB: 1/1/90

再次打印:


学生姓名:学生
学生姓氏:默认
学生ID:0
学生DoB:1/1/90

Student name: Student Student surname: Default Student ID: 0 Student DoB: 1/1/90


推荐答案

readObject()不在流结束时返回 null ,因此在循环终止条件无效时对其进行测试。您的代码正确在遇到流结束时抛出 EOFException 。这里没有问题需要解决。

readObject() doesn't return null at end of stream, so testing for it as your loop termination condition is invalid. Your code is correctly throwing EOFException when it encounters end of stream. There is no problem here to solve.

这篇关于Java - 序列化 - EOFException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆