2016-05-18 74 views
0

我一直在尝试保存和加载对象,但它不工作。当我告诉它保存,我有这样的:保存/加载不适用于Android

public void doSave(View v) { 
    ObjectOutputStream out; 
    try { 
     FileOutputStream outFile = new FileOutputStream("testThingy321.ser"); 
     out = new ObjectOutputStream(outFile); 
     out.writeObject(hero); 
     out.close(); 
     outFile.close(); 
    } catch (Exception e) {e.printStackTrace();} 
} 

当我告诉它加载,我有这样的:

try { 
      FileInputStream fileIn = new FileInputStream("testThingy321.ser"); 
      ObjectInput in = new ObjectInputStream(fileIn);  
      hero = (Protag) in.readObject(); 
      in.close(); 
      fileIn.close(); 
     } catch (Exception e) {e.printStackTrace();} 

我没有这个如何工作的一个很好的理解不够告诉它为什么不起作用,这几乎是我发现的样品告诉我要说的。但是,它似乎从未做过任何事情。有人能告诉我为了让它保存或加载我必须做些什么吗?

编辑:我检查了logcat的,我得到这个:

05-17 22:25:46.0​​98:W/System.err的(24774):java.io.FileNotFoundException:testThingy321.ser:开放失败:EROFS(只读文件系统)

+1

定义readObjectwriteObject方法检查logcat中,最有可能有错误被打印出来,你是不是看到他们 – Nullpoint

回答

1

你Protag类应该实现Serializable接口,并在此描述Serialization - readObject writeObject overrides

+0

可能会有帮助,但它仍然是不工作。 – aattss

+0

请尝试此操作以查找您的文件 String filePath = context.getFilesDir()。getPath()。toString()+“/ "testThingy321.ser”)“; FileOutputStream outFile = new FileOutputStream(new File(filePath); –