如何将一个类的对象存储到内部存储器存储使用序列化? [英] How to store a class object into Internal Memory Storage using serializable?
问题描述
我需要这个对象存储到手机的内部存储内存,和我有<使用-权限的Android:名称=android.permission.WRITE_INTERNAL_STORAGE>< /用途-permission>
货单上设置。对象本身富人两个静态方法存储和从内部存储器中重新加载它:
I need to store this object into the internal storage memory of the phone, and i have the <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"></uses-permission>
set on the manifest. The object itself haves two static methods for store and reload it from the internal memory:
public class SaveState implements Serializable {
static public List<FullMagazine> fms = new ArrayList<FullMagazine>();
static SaveState instance=null;
public static SaveState getInstance(){
if( instance == null )
instance = new SaveState();
return instance;
}
public static void saveData(SaveState instance){
ObjectOutput out;
try {
out = new ObjectOutputStream(new FileOutputStream("appSaveState.data"));
out.writeObject(instance);
out.close();
} catch (Exception e) {e.printStackTrace();}
}
public static SaveState loadData(){
ObjectInput in;
SaveState ss=null;
try {
in = new ObjectInputStream(new FileInputStream("appSaveState.data"));
ss=(SaveState) in.readObject();
in.close();
} catch (Exception e) {e.printStackTrace();}
return ss;
}
}
我试图来存储并使用该电话在我的活动重新打开:
I'm trying to store it and reopen it using this call in my activity:
SaveState.fms.add(fm);
SaveState.saveData(SaveState.getInstance());
SaveState sv = SaveState.loadData();
但它不工作,它不是存储的对象,ofcourse它不读objecto,我得到这两个例外,同时存储和读取的对象:
But it is not working, it is not storing the object, and ofcourse it is not reading the objecto, i'm getting these two exception while storing and reading the object:
03-23 09:18:16.702: WARN/System.err(9060): java.io.FileNotFoundException: /appSaveState.data (Read-only file system)
03-23 09:18:16.702: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-23 09:18:16.702: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
03-23 09:18:16.702: WARN/System.err(9060): at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
03-23 09:18:16.702: WARN/System.err(9060): at com.Magazine.SaveState.saveData(SaveState.java:35)
03-23 09:18:16.702: WARN/System.err(9060): at com.Magazine.MainMenu$DownloadThread.run(MainMenu.java:807)
03-23 09:18:16.709: WARN/System.err(9060): java.io.FileNotFoundException: /appSaveState.data (No such file or directory)
03-23 09:18:16.709: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-23 09:18:16.709: WARN/System.err(9060): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-23 09:18:16.709: WARN/System.err(9060): at java.io.FileInputStream.<init>(FileInputStream.java:82)
03-23 09:18:16.709: WARN/System.err(9060): at java.io.FileInputStream.<init>(FileInputStream.java:134)
03-23 09:18:16.709: WARN/System.err(9060): at com.Magazine.SaveState.loadData(SaveState.java:46)
03-23 09:18:16.709: WARN/System.err(9060): at com.Magazine.MainMenu$DownloadThread.run(MainMenu.java:809)
什么是错在code?
What's wrong in the code?
感谢
推荐答案
改变这种
out = new ObjectOutputStream(new FileOutputStream("appSaveState.data"));
与
File outFile = new File(Environment.getExternalStorageDirectory(), "appSaveState.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
为正确地指出@ex,该文件将不会被删除清除应用程序的数据或卸载应用程序
as correctly pointed out by @e-x, the file will not be removed clearing application's data or uninstalling the app
这篇关于如何将一个类的对象存储到内部存储器存储使用序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!