简体   繁体   English

ObjectInputStream.readObject()给出EOFException

[英]ObjectInputStream.readObject() gives EOFException

I'm completely new to programming, so I'm having a difficult time resolving my own errors. 我是编程的新手,所以我很难解决自己的错误。 Someone advised me to try it on this website, so I thought why not give it a shot. 有人建议我在这个网站上尝试一下,所以我想为什么不试一试。

The other posts that I found regarding this error didn't seem very relevant: most were people advising to close the input stream, but my code already does that. 我发现的与此错误有关的其他帖子似乎并不相关:大多数人建议关闭输入流,但是我的代码已经做到了。

What I want it to do: Write a Photo object called "photo" to a file called "test.ser". 我要执行的操作:将一个名为“ photo”的Photo对象写入一个名为“ test.ser”的文件。 Then read the file "test.ser" AND return the path of the object ("photo") in "test.ser" back to me. 然后读取文件“ test.ser”,并将“ test.ser”中对象(“照片”)的路径返回给我。

What it actually does: Writes a Photo object called "photo" to "test.ser". 它的实际作用:将一个名为“ photo”的Photo对象写入“ test.ser”。 Reads "test.ser", returns an EOFException and no path. 读取“ test.ser”,返回EOFException且没有路径。

Returning the path isn't actually very important, as long as it returns something of value to me. 只要返回路径对我来说有价值,返回路径实际上并不很重要。 But I am getting the same error when I use "System.out.println(photo)" or "photo.getId()". 但是当我使用“ System.out.println(photo)”或“ photo.getId()”时,出现了相同的错误。

I'm not very sure what I need to paste here, so I will post the two try/catch-es that I use for serializing and deserializing the object: 我不太确定需要在此处粘贴什么,因此我将发布两个我用于序列化和反序列化对象的try / catch-es:

Serializing object: 序列化对象:

    File test = new File("path.../something.ser");
    Photo photo = new Photo(2, "..\\images\\2.jpg", getImage("..\\images\\2.jpg"));

    try {
        FileOutputStream fos = new FileOutputStream(test);
        ObjectOutputStream out = new ObjectOutputStream(fos);

        if (!test.exists()) {
            test.createNewFile();
        }
        out.writeObject(photo);
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

Deserializing object: 反序列化对象:

        try {
        FileInputStream fis = new FileInputStream(test);
        ObjectInputStream in = new ObjectInputStream(fis);

        in.readObject();

        photo = (Photo)in.readObject();
        photo.getPath();
        in.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

And the error: 错误:

run:
null
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 view.Main.<init>(Main.java:103) 
  //103 is the line that casts the input object to a Photo object.

BUILD SUCCESSFUL (total time: 1 second)

From what I unstander the error occurs when I am trying to type cast the object - that I receive through the method readObject - to a "photo" class object. 从我的不理解中,当我尝试将通过方法readObject收到的对象类型转换为“照片”类对象时,发生了错误。 At least, that's what the error at line 103 is refering to. 至少,这就是第103行的错误所指。

I read elsewhere that the error means that I "tried to read more objects than there actually are there". 我在其他地方读到该错误意味着我“尝试读取的对象多于实际存在的对象”。 Not sure what that means though, because I just want it to read 1 image - which should be within the object - and return its location. 但是不确定那是什么意思,因为我只希望它读取1张图像-应该在对象内-并返回其位置。

Also I read that ObjectInputStream never returns null, unless I gave that value somewhere. 另外我读到ObjectInputStream永远不会返回null,除非我在某个地方给出了该值。 But it actually is returning(?) "null", even though my code doesn't contain a null value... 但这实际上是返回(?)“ null”,即使我的代码不包含null值...

I've been at it for days now (yes I am just that bad) and still no luck. 我已经去了好几天了(是的,我只是那样的糟糕)而仍然没有运气。

You read it twice: 您阅读了两次:

    in.readObject();

    photo = (Photo)in.readObject();

Remove the first line. 删除第一行。 Also you don't have to create the file. 另外,您不必创建文件。 The output stream will do that for you. 输出流将为您完成此任务。

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

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