我使用序列化來保存我的android應用程序中的數據。一切都很好,直到用戶報告他無法打開他的文件。
檢查完他發給我的文件後,發現readObject()時出現EOFException。EOFException當在Android應用程序中的readObject
我在vim中打開了.ser文件,它不是空的。它看起來和其他可以正常打開的文件一樣正常。
用戶保存.ser文件時不會發生意外。
我的問題是:
1.首先使用序列化是錯誤的決定嗎?
2.我的代碼在大多數情況下都能正常工作,但我可能錯過了最終導致此問題的原因。
3.用戶說這個文件對他來說是非常重要的,所以我真的想找到解決辦法來打開這個文件。
這裏是代碼的文件是如何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();
這裏是我使用的保存序列化的代碼。
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.
}
我檢查了代碼句柄的異常,它捕獲並忽略了IOException。你是對的,也許是一個例外。但我無法想象發生了什麼樣的異常?下一次寫入時發生IOException,請求用戶「重試」或「取消」會有幫助嗎? – 2011-05-28 04:33:19