Java 如何使用可序列化将类对象存储到内部存储器中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9836663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store a class object into Internal Memory Storage using serializable?
提问by NullPointerException
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:
我需要将此对象存储到手机的内部存储器中,并且我<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;
}
}
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();
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)
What's wrong in the code?
代码有什么问题?
Thanks
谢谢
采纳答案by Blackbelt
change this
改变这个
out = new ObjectOutputStream(new FileOutputStream("appSaveState.data"));
with
和
File outFile = new File(Environment.getExternalStorageDirectory(), "appSaveState.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
as correctly pointed out by @e-x, the file will not be removed clearing application's data or uninstalling the app
正如@ex 正确指出的那样,该文件不会被删除清除应用程序的数据或卸载应用程序
回答by Tim
You're not allowed to just write into the internal storage (even if you get that permission). Try it with the external sd or your cache folderinstead.
您不能只写入内部存储(即使您获得该权限)。尝试使用外部 sd 或您的缓存文件夹。
回答by Aerim
This was my aproach based on this post and this
这是我基于这篇文章和这篇文章的方法
I wrote this on my User class
我在我的 User 类上写了这个
private static File mFolder;
public void saveData(Activity pContext) {
//this could be initialized once onstart up
if(mFolder == null){
mFolder = pContext.getExternalFilesDir(null);
}
this.save();
ObjectOutput out;
try {
File outFile = new File(mFolder,
"someRandom.data");
out = new ObjectOutputStream(new FileOutputStream(outFile));
out.writeObject(this);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static User loadData(Context pContext) {
if(mFolder == null){
mFolder = pContext.getExternalFilesDir(null);
}
ObjectInput in;
User lUser = null;
try {
FileInputStream fileIn = new FileInputStream(mFolder.getPath() + File.separator + "someRandom.data");
in = new ObjectInputStream(fileIn);
lUser = (User) in.readObject();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
if (lUser != null) {
lUser.save();
Log.d("current User: ", lUser.nickname);
} else {
Log.d("current User: ", "null");
}
return lUser;
}
Edit:
编辑:
Then from my activity i call
然后从我的活动中我打电话
mUser = User.loadData(mSelf);
or
或者
mUser = User.loadData(this);
mSelf would be an instance I store
mSelf 将是我存储的一个实例
Hope this helps :)
希望这可以帮助 :)