【问题标题】:What is wrong with creating a new .ser file in this method?用这种方法创建一个新的 .ser 文件有什么问题?
【发布时间】:2018-07-29 05:28:15
【问题描述】:

如果还没有对象,我正在尝试创建一个新的 .ser 文件来存储对象。当它运行时,它会抛出一个 EOFException。 EOFException 到底是什么,这个方法是否正确编写来创建和读取 .ser 文件?感谢您的任何反馈。

public void readDatabase() throws IOException {

        File dataFile = new File("database.ser");

        // If data file does not exist, create it.
        if (!dataFile.exists()) {
            System.out.println("database.ser does not exist, creating one now . . .");
            // if the file doesn't exists, create it        
            dataFile.createNewFile();
            return; // No need to try to read anything from an empty file, so return.
        }
        ObjectInputStream objectinputstream = null;
        boolean cont = true;
        try {
            FileInputStream streamIn = new FileInputStream(dataFile);
            objectinputstream = new ObjectInputStream(streamIn);
            while (cont) {
                Item obj = null;
                try {
                    obj = (Item) objectinputstream.readObject();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                if (obj != null)
                    itemList.add(obj);
                else
                    cont = false;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (objectinputstream != null) {
                objectinputstream.close();
            }
        }

    }

EOF异常:

java.io.EOFException
    at java.base/java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2758)
    at java.base/java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:3253)
    at java.base/java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
    at java.base/java.io.ObjectInputStream.<init>(ObjectInputStream.java:343)
    at hardwarestore.HardwareStore.readDatabase(HardwareStore.java:254)
    at hardwarestore.HardwareStore.<init>(HardwareStore.java:33)
    at hardwarestore.MainApp.<init>(MainApp.java:24)
    at hardwarestore.MainApp.main(MainApp.java:259)

【问题讨论】:

  • 你用谷歌搜索过异常吗?

标签: java object serialization


【解决方案1】:

EOFException 代表:

文件结束异常

当您尝试从 FileReader 已到达文件末尾的文件中读取数据时,通常会发生这种情况。换句话说,没有更多数据要读取。

您应该捕获异常并关闭您的流。因为它表明您已读取文件中的所有对象。参考answer in this question

 while (true) {
        try {
            // Read the next object from the stream. If there is none, the
            // EOFException will be thrown.

            Item obj = (Item) objectinputstream.readObject();
            itemList.add(obj);
        } catch (EOFException e) {
            // If there are no more objects to read, return what we have.
            return contactMap;
        } finally {
            // Close the stream.
            in.close();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-10
    • 2015-04-14
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 2016-09-12
    相关资源
    最近更新 更多