在Android中读取序列化文件时出现EOFException

时间:2014-02-20 19:56:37

标签: android serialization eofexception

我一直在寻找这个问题的答案,但其他人的答案都不适合我。我在android中使用序列化,我序列化一个对象。问题是,当我反序列化它时,我得到EOFException并且对象将不会加载。 我的保存代码:

FileOutputStream fileOut = null;
try {
    fileOut = context.openFileOutput("state.ser", Context.MODE_PRIVATE);
    ObjectOutputStream out = new ObjectOutputStream(fileOut);
    out.writeObject(MyObject);
    out.close();
    fileOut.close();
} catch (IOException i) {
    i.printStackTrace();
}

我的加载类抛出异常:

FileInputStream fis;
try {
    fis = context.openFileInput("state.ser");
    ObjectInputStream is = new ObjectInputStream(fis);
    myObject = (MyObject) is.readObject(); //exception thrown from here
    is.close();
} catch (FileNotFoundException e) {
    generateMyObject();
} catch (Exception e) {
    e.printStackTrace();
}

我的对象的定义:

public class MyObject implements Serializable {
    private static final long serialVersionUID = 155986L;   
    public MyInnerObject[][] mio = null;
    public Boolean[] unassigned = null; 
}
public class MyInnerObject implements Serializable {
    private static final long serialVersionUID = 1L;    
    public boolean[] legals;
    public int ID;
    public int pos;
}

我做错了什么?谢谢你的所有答案。

编辑:堆栈跟踪

java.io.EOFException
java.io.DataInputStream.readBoolean(DataInputStream.java:69)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1462)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readNewArray(ObjectInputStream.java:1490)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:760)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
java.io.ObjectInputStream.readFieldValues(ObjectInputStream.java:1115)
java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:455)
java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1347)
java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1244)
java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1833)
java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:762)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
com.example.myApp.Cell.loadState(Cell.java:224)
... rest is only where in app it appeared

即使这只是警告,对象也不会加载,所以我无法使用它。

2 个答案:

答案 0 :(得分:1)

我不知道这是否对您有所帮助,但here它表示Android中的序列化可能会因大型整数数组而出错。 在你的情况下,你有一个布尔数组,无论如何都是一个整数。

我会尝试通过剥离一些可序列化的数据来进行测试(例如,仅序列化部分信息),以查看问题是否与数组有关。

此外,这可能是显而易见的,但可能不是......确保您没有加载已使用不同版本的对象保存的文件(例如,您保存对象进行一些测试,然后添加了变量到你的班级,现在你正试图反序列化。

答案 1 :(得分:0)

文件为空或短。创建它时必须有例外。