【问题标题】:Reading an object via ObjectInputStream throws EOF exception通过 ObjectInputStream 读取对象会引发 EOF 异常
【发布时间】:2015-12-21 14:19:34
【问题描述】:

我正在尝试通过ObjectInputStream 读取对象。但是,我使用EOFException 检索以下堆栈跟踪:

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
at
java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
at student.operations.StudentOperations.addStudentDetails(StudentOperations.java:46)
at student.

我的代码如下:

FileInputStream fi = new        FileInputStream(file.getPath());
ObjIn = new ObjectInputStream(fi);

FileOutputStream fo = new FileOutputStream(file.getPath());
Objout = new ObjectOutputStream(fo);

//bo = new BufferedOutputStream(Objout);
//bi = new BufferedInputStream(ObjIn);

System.out.println("Enter the number of students to add");

number = in.nextInt();

System.out.println("Enter Student roll number:");
int rollno = in.nextInt();

try {
    HashMap<Integer, StudentModel> hs = (HashMap<Integer, StudentModel>) ObjIn
            .readObject();

    if (hs.containsKey(rollno)) {
        System.out.println("Duplicate values are not allowed ");
    } else {

        for (counter = 0; counter < number; counter++) {

            System.out.println("Enter Student name:");
            String name = in.next();

            System.out.println("Enter Student's Father name:");
            String fname = in.next();

            System.out.println("Enter Gender:");
            String gender = in.next();

            System.out.println("Enter Date of birth:");
            String date = in.next();

            SimpleDateFormat dateFormat = new SimpleDateFormat(
                    "dd-MM-yyyy");
            Date dateObj = null;

            try {
                dateObj = dateFormat.parse(date);
            } catch (ParseException e) {
                System.out.println(e);
            }

            Calendar birthday = Calendar.getInstance();

            birthday.setTimeInMillis(dateObj.getTime());

            Calendar current = Calendar.getInstance();

            long currentTime = System.currentTimeMillis();

            current.setTimeInMillis(currentTime);

            int age = current.get(Calendar.YEAR)
                    - birthday.get(Calendar.YEAR);

            System.out.println("Enter the AddressLine1:");
            String address1 = in.next();
            in.nextLine();

            System.out.println("Enter the AddressLine2:");
            String address2 = in.next();
            in.nextLine();

            System.out.println("Enter the city");
            String city = in.next();
            in.nextLine();

            System.out.println("Enter the state");
            String state = in.next();
            in.nextLine();

            System.out.println("Enter the country:");
            String country = in.next();
            in.nextLine();

            System.out.println("Enter the Zipcode");
            int code = in.nextInt();
            in.nextLine();

            StudentUtill.student.put(rollno, new StudentModel(name,
                    fname, rollno, age, gender, dateObj, address1,
                    address2, city, state, country, code));

            Objout.writeObject(StudentUtill.student.toString());


            Objout.flush();

        }// for loop ends here

tester.StudentTester.main(StudentTester.java:30)

【问题讨论】:

  • 这是哪一行? StudentOperations.java:46
  • 如果你得到一个 EOF,你很可能一开始就没有正确序列化你的 HashMap

标签: java serialization file-io deserialization objectinputstream


【解决方案1】:
  • 您正试图从空文件中读取对象。查看堆栈跟踪。试图读取标题的文件结尾。文件中甚至没有标题,更不用说对象了。是空的。

  • 在你有东西要写之前不要创建FileOutputStream,之后不要忘记立即关闭它。

  • 在将地图写入文件之前,您还错误地将地图转换为 String,但您还没有达到问题的地步。

【讨论】:

  • 能否请您解释一下如何以有效的方式执行对象序列化。
  • @TheRed 如果您有一个新问题,您应该将其作为一个新问题提出。没有人会在这里看到它。我没有,两年了。
  • @Visionwriter 1. 我不同意这个答案有任何问题,或者它以任何方式与您的描述相对应,特别是我不同意实际代码有助于解释任何事情在这个问题中,一般来说,这不是,我也不是,免费的编码服务。 2. 继续搜索而不是在 cmets 中开始无关的对话或新问题,这正是“可怜的家伙”在这个网站上应该做的事情。 3. 请勿在此发表个人言论。
猜你喜欢
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 2016-04-30
  • 1970-01-01
  • 2016-06-28
  • 2011-07-19
  • 2013-07-13
相关资源
最近更新 更多