我需要将这个对象存储到手机的内部存储器中,并且我在清单文件中设置了
我正在尝试使用以下代码在我的活动中储存并重新打开它:
代码出了什么问题吗?
谢谢。
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"></uses-permission>
。该对象本身具有两个静态方法,用于从内部存储器中存储和重新加载它: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;
}
}
我正在尝试使用以下代码在我的活动中储存并重新打开它:
```
SaveState.fms.add(fm);
SaveState.saveData(SaveState.getInstance());
SaveState sv = SaveState.loadData();
但它并没有起作用,它没有存储该对象,当然也没有读取该对象。在存储和读取对象时,我得到了这两个异常:
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)
代码出了什么问题吗?
谢谢。
FullMagazine
中使用的类是否也实现了Serializable接口? - Sharp Edge