【问题标题】:Saving/loading isn't working on Android保存/加载在 Android 上不起作用
【发布时间】:2016-09-14 07:10:43
【问题描述】:

我一直在尝试保存和加载一个对象,但它不起作用。当我告诉它保存时,我有这个:

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.098:W/System.err(24774):java.io.FileNotFoundException:testThingy321.ser:打开失败:EROFS(只读文件系统)

【问题讨论】:

  • 检查你的logcat,很可能有错误被打印出来而你没有看到它们

标签: android save


【解决方案1】:

您的 Protag 类应实现 Serializable 接口并定义 readObjectwriteObject 方法,如此处所述 Serialization - readObject writeObject overrides

【讨论】:

  • 试试这个为了找到你的文件 String filePath = context.getFilesDir().getPath().toString() + "/"testThingy321.ser")"; FileOutputStream outFile = new FileOutputStream(new File(filePath);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 2011-07-26
相关资源
最近更新 更多