java ObjectInputStream 抛出 EOFException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15171184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:42:43  来源:igfitidea点击:

ObjectInputStream throws EOFException

javafilefile-iofilereaderserializable

提问by ams

I'm writing a class to keep track of competition data at a tournament. I want to store this class to a file, so am using an ObjectInputStream. The class I am writing implements Serializable. I'm getting an EOFException, and none of the solutions I've found on SO and elsewhere actually address this issue.

我正在编写一个类来跟踪锦标赛中的比赛数据。我想将这个类存储到一个文件中,所以我使用了一个ObjectInputStream. 我正在编写的类实现了可序列化。我得到了一个EOFException,我在 SO 和其他地方找到的解决方案都没有真正解决这个问题。

My file writer is:

我的文件编写器是:

public void writeToFile(String path) {
    File f = new File(path);
    if(f.exists()) f.delete();

    try {
        OutputStream fileOut = new FileOutputStream(path);
        OutputStream bufferOut = new BufferedOutputStream(fileOut);
        ObjectOutput output = new ObjectOutputStream(bufferOut);

        output.writeObject(this);
    } catch(IOException e) {}
}

My file reader is:

我的文件阅读器是:

public static DivisionDataFTC readFromFile(String path) {
    try {
        InputStream fileIn = new FileInputStream(path);
        InputStream bufferIn = new BufferedInputStream(fileIn);
        ObjectInput input = new ObjectInputStream(bufferIn);

        System.out.println(input.read());
    } catch(Exception e) {
        System.out.println(path);
        e.printStackTrace();
    }

    if(1==1) throw new Error("Could not read DivisionDataFTC at " + path);
    return null;
}

I write the data successfully - I have verified that the file is notempty. (Its contents, if relevant, are consistently 7.99kb).

我成功写入数据 - 我已验证文件不为空。(如果相关,其内容始终为 7.99kb)。

To be clear, the error does not occur on the instantiation of the ObjectOutputStream. This is what makes this question different - the error occurs on the readObject() call. My output is a remarkably long EOFException:

需要明确的是,该错误不会发生在 ObjectOutputStream 的实例化上。这就是这个问题的不同之处 - 错误发生在 readObject() 调用上。我的输出非常长EOFException

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 java.util.ArrayList.readObject(ArrayList.java:733)
    (...cut out most of this because nobody wants to read it...)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:369)
    at tournamentftc.DivisionDataFTC.readFromFile(DivisionDataFTC.java:297)
    at firstscouting.FIRSTScouting.runGUI(FIRSTScouting.java:82)
    at firstscouting.FIRSTScouting.main(FIRSTScouting.java:101)
Exception in thread "main" java.lang.Error: Could not read DivisionDataFTC at C:\Users\Noah\Desktop\out.ser
    at tournamentftc.DivisionDataFTC.readFromFile(DivisionDataFTC.java:303)
    at firstscouting.FIRSTScouting.runGUI(FIRSTScouting.java:82)
    at firstscouting.FIRSTScouting.main(FIRSTScouting.java:101)

I'm not sure why this is happening. How do I handle this?

我不确定为什么会这样。我该如何处理?

回答by Evgeniy Dorofeev

You should close ObjectOutputStream, try

你应该关闭 ObjectOutputStream,试试

 ObjectOutput output = new ObjectOutputStream(bufferOut);
 output.writeObject(this);
 output.close();

回答by ams

You need to close your output stream, and you also nee to call readObject() not read.

您需要关闭输出流,并且您还需要调用 readObject() 未读。