简体   繁体   English

为什么会出现java.io.EOFException?

[英]Why am I getting java.io.EOFException?

This is a method to read show details from a .ser file containing serialized objects of type Show. 这是一种从.ser文件读取显示详细信息的方法,该文件包含Show类型的序列化对象。 The method successfully return the list but gives an Exception before. 该方法成功返回列表,但之前给出了Exception。 Why is it so and how do I get rid of it? 为什么会这样,我该如何摆脱呢?

public List<Show> populateDataFromFile(String fileName) {
        List<Show> shows=new ArrayList<Show>();
        ObjectInputStream obj=null;
    try {
        FileInputStream fin=new FileInputStream(fileName);
        obj=new ObjectInputStream(fin);
        Show show=null;
        while((show=(Show) obj.readObject())!=null)
        {
            shows.add(show);
            show.getShowName();
        }
        System.out.println(shows);
    } catch (IOException e) {
        e.printStackTrace();
    }catch(ClassNotFoundException e)
    {
        e.printStackTrace();
    }finally
    {
        try {
            obj.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return shows;
}

The output is 输出是

java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2571)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1315)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
at com.util.DataManagerImpl.populateDataFromFile(DataManagerImpl.java:23)
at com.psl.Client.main(Client.java:9)
Show Name: Sahi re Sahi
Show Time: 6:30 PM
Seats Available: 40
Show Name: Ek Shyam Aapke Naam
Show Time: 6:30 PM
Seats Available: 40
Show Name: Moruchi Maushi
Show Time: 6:30 PM
Seats Available: 40
Show Name: All The Best
Show Time: 6:30 PM
Seats Available: 40
Show Name: Naksharanche Dene
Show Time: 6:30 PM
Seats Available: 40

The main method is 主要方法是

public static void main(String[] args) {
    DataManager dm=new DataManagerImpl();
    List<Show>shows=dm.populateDataFromFile("ShowDetails.ser");
    // Call all the functionalities from here to test your code.    
    for(Show show:shows)
    {
        System.out.println("Show Name: "+show.getShowName());
        System.out.println("Show Time: "+show.getShowTime());
        System.out.println("Seats Available: "+show.getSeatsAvailable());
    }
}

Because you've reached the end of the file. 因为您已到达文件末尾。

You seem to think that readObject() returns null at end of file. 您似乎认为readObject()在文件末尾返回null。 It doesn't. 没有。 It returns null if and only if you wrote a null. 当且仅当您写入null时,它才返回null。

The correct test is to catch EOFException and break, not to read until readObject() returns null. 正确的测试是捕获EOFException并中断,直到readObject()返回null才读取。

EJP is correct that the error happens because the method readObject() does not return NULL when there is no more Show in the file. EJP是正确的,因此会发生错误,因为文件中没有更多Show时,方法readObject()不会返回NULL。

EJP also has suggested a solution. EJP还提出了解决方案。 I just want to suggest several other solutions, but instead of fixing the code that read the file, the solutions fix the code that write the file. 我只想提出其他几种解决方案,但是这些解决方案不是修复读取文件的代码,而是修复写入文件的代码。

Currently, the file is generated by calling writeObject() method multiple times, each with a Show object. 当前,通过多次调用writeObject()方法来生成文件,每个方法都有一个Show对象。 Instead of doing this you can: 除了执行此操作,您还可以:

  1. Create a List object that holds the shows. 创建一个包含节目的List对象。 Write this List into file. 将此List写入文件。 When you read this file, you can just read the List back. 读取此文件时,您只需读回List You don't have to use while loop, and therefore, avoid the EOF exception check. 您不必使用while循环,因此避免EOF异常检查。

  2. Write the number of shows first to the file. 首先将显示次数写入文件。 After that, write the shows, one by one, to the file. 之后,将节目一个接一个地写入文件。 When you read the file, you need to first read the number of shows, which informs you how many shows to read from the file. 读取文件时,您需要先读取节目的数量,这会告诉您要从文件中读取多少个节目。 This will avoid the EOF exception check. 这样可以避免EOF异常检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM