Android 应用程序中 readObject 时出现 EOFException

发布于 2024-11-09 21:55:18 字数 1672 浏览 0 评论 0原文


我使用序列化在我的 Android 应用程序中保存数据。一切都工作得很好,直到用户报告他无法打开他的文件。
检查他发送给我的文件后,发现问题是 readObject() 时出现 EOFException。

我在vim中打开.ser文件,它不是空的。看起来和其他文件一样正常,可以正常打开。
当用户保存 .ser 文件时,并没有什么意外发生。

我的问题是:
1. 首先使用序列化是一个错误的决定吗?
2.我的代码在大多数情况下都可以正常工作,但我可能错过了一些最终导致此问题的东西。
3. 用户说该文件对他来说非常重要,所以我真的想找到一种解决方法来打开该文件。

这是如何读取文件的代码:

    FileInputStream f_in = null;
    ObjectInputStream obj_in = null;

    f_in = new FileInputStream(fileName);
    obj_in = new ObjectInputStream (f_in);
    mBlock = (Block)obj_in.readObject();

这是我用于保存可序列化的代码。

saveObjToFile(mBlock, mFileName);

static void saveObjToFile(Serializable object, String fileName)                     
throws IOException{
    FileOutputStream f_out = null;
    ObjectOutputStream obj_out  = null;
    try{
        File note_file = new File(fileName);
        f_out = new FileOutputStream(note_file);
        obj_out = new ObjectOutputStream (f_out);
        obj_out.writeObject(object);
        obj_out.flush();
        obj_out.close();
    }finally{
        if (obj_out != null)
            try{
                obj_out.close();
            }catch(IOException e){

            }
        if (f_out != null)
            try{
                f_out.close();
            }catch(IOException e){

            }
    }
}

这是块定义的代码片段:

public class Block implements Serializable{
        private static final long serialVersionUID = -4369689728261781215L;
        private int mType;
        private byte[] mContent;
        private ArrayList<Block> mChildren = null;
        //... no more attributes.
}

I use serialization to save data in my android app. Everything works beautifully until a user report he can not open his file.
After examined the file he sent to me, it turned out the problem is an EOFException when readObject().

I opened the .ser file in vim, it is not empty. It looks as normal as other files which can be opened normally.
No surprise happened when the user saved the .ser file.

My questions are:
1. Is it a wrong decision to use serialization in the first place?
2. My code works normally in most of the cases, but I may have missed something which eventually cause this problem.
3. The user said the file is INCREDIBLY important for him, so I really want to find a work around to open this file.

Here is the code how the file is readed:

    FileInputStream f_in = null;
    ObjectInputStream obj_in = null;

    f_in = new FileInputStream(fileName);
    obj_in = new ObjectInputStream (f_in);
    mBlock = (Block)obj_in.readObject();

Here is the code I use for saving a serializable.

saveObjToFile(mBlock, mFileName);

static void saveObjToFile(Serializable object, String fileName)                     
throws IOException{
    FileOutputStream f_out = null;
    ObjectOutputStream obj_out  = null;
    try{
        File note_file = new File(fileName);
        f_out = new FileOutputStream(note_file);
        obj_out = new ObjectOutputStream (f_out);
        obj_out.writeObject(object);
        obj_out.flush();
        obj_out.close();
    }finally{
        if (obj_out != null)
            try{
                obj_out.close();
            }catch(IOException e){

            }
        if (f_out != null)
            try{
                f_out.close();
            }catch(IOException e){

            }
    }
}

Here is a code snippet of Block definition:

public class Block implements Serializable{
        private static final long serialVersionUID = -4369689728261781215L;
        private int mType;
        private byte[] mContent;
        private ArrayList<Block> mChildren = null;
        //... no more attributes.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七堇年 2024-11-16 21:55:18

在保存可序列化的代码中,异常在哪里处理?

我认为可以安全地假设,因为您无法读取该文件,所以在写入文件时引发了异常。在我看来,如果您在遇到错误后只是关闭输出流,是否能够恢复文件是值得怀疑的。

In the code for saving the serializable, where is the exception handled?

I think its safe to assume, because you can't read the file, that an exception was raised while the file was being written. It is doubtful, in my mind, that you will be able to recover the file if you simply closed the outputstream after encountering an error.

赤濁 2024-11-16 21:55:18

EOFException 是正常!它只是意味着您到达了对象流的末尾。如果在读取第一个对象时得到它,则流中没有对象。请注意,它确实有一个对象输出流标头,否则您会得到不同的异常,因此它不为空。只是空无一物。

EOFException is normal! it just means you got to the end of the object stream. If you get it when reading the first object, the stream has no objects in it. Note that it does have an object output stream header, otherwise you would have got a different exception, so it isn't empty. Just empty of objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文