java - 通过 ObjectInputStream 读取对象会抛出 EOF 异常

标签 java serialization file-io deserialization objectinputstream

我正在尝试通过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)

最佳答案

  • 您正在尝试从空文件中读取对象。查看堆栈跟踪。尝试读取 header 的文件结尾。文件中甚至没有 header ,更不用说对象了。里面是空的。

  • 在有内容要写入之前不要创建 FileOutputStream,并且不要忘记在写入后立即关闭它。

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

关于java - 通过 ObjectInputStream 读取对象会抛出 EOF 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34397815/

相关文章:

java - 无法声明注释数组

java - 通过 Java RMI 发送加密对象

c# - XMLSerializer 到 XElement

java - 为什么此实用程序方法会使文件处于锁定状态?

vba - Excel 2007 VBA 宏逐行读取文本文件如何停止逗号 (,) 分隔

java - iReport 中的错误日期破坏了 jasper 服务器报告

Javafx - 如何访问 FXML "objects"

c# - 我可以简单地 'read' 一个正在使用的文件吗?

java - 如何更改 GWT 的小部件 CSS 值?

java - 如何序列化第三方不可序列化的最终类(例如谷歌的 LatLng 类)?