android如何将对象保存到文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3625496/
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
android how to save an object to file?
提问by Fabien
Does someone know how to save and restore an object to a file on android ?
有人知道如何将对象保存和恢复到 android 上的文件吗?
采纳答案by Fabien
See here for a code example : android how to save a bitmap - buggy code
有关代码示例,请参见此处:android how to save a bitmap - buggy code
回答by yayay
Open a file using openFileOutput() ( http://developer.android.com/guide/topics/data/data-storage.html#filesInternal), than use an ObjectOutputStream ( http://download.oracle.com/javase/1.4.2/docs/api/java/io/ObjectOutputStream.html) to write your object to a file.
使用 openFileOutput() ( http://developer.android.com/guide/topics/data/data-storage.html#filesInternal)打开文件,而不是使用 ObjectOutputStream ( http://download.oracle.com/javase/ 1.4.2/docs/api/java/io/ObjectOutputStream.html) 将您的对象写入文件。
Use their evil twin openFileInput() and ObjectInputStream() to reverse the process.
使用他们邪恶的孪生 openFileInput() 和 ObjectInputStream() 来逆转这个过程。
回答by matekm
It depends if You want to save file on internal or external media. For both situation there are great samples on Android DEV site: http://developer.android.com/guide/topics/data/data-storage.html- this should definetly help
这取决于您是要将文件保存在内部媒体还是外部媒体上。对于这两种情况,Android DEV 站点上都有很棒的示例:http: //developer.android.com/guide/topics/data/data-storage.html- 这肯定会有所帮助
回答by Azurespot
Here is a tested example of @yayay's suggestion. Note that using readObject()
returns an Object
, so you will need to cast, although the compiler will complain that it is an unchecked cast. I can still run my code fine though. You can read more about the casting issue here.
这是@yayay 建议的经过测试的示例。请注意, usingreadObject()
返回 an Object
,因此您需要进行强制转换,尽管编译器会抱怨它是未经检查的强制转换。不过,我仍然可以正常运行我的代码。您可以在此处阅读有关铸造问题的更多信息。
Just make sure that your class (in my case, ListItemsModel
) is serializable, because the writeObject()
will serialize your object, and the readObject()
will deserialize it. If it is not (you get no persistence and the logcat throws a NotSerializableException
), then make sure your class implements java.io.Serializable
, and you're good to go. Note, no methods need implementing in this interface. If your class cannot implement Serializable
and work (e.g. 3rd party library classes), this linkhelps you to serialize your object.
只需确保您的类(在我的情况下,ListItemsModel
)是可序列化的,因为writeObject()
将序列化您的对象,readObject()
并将反序列化它。如果不是(您没有持久性并且 logcat 会抛出NotSerializableException
),那么请确保您的类实现了java.io.Serializable
,然后您就可以开始了。请注意,此接口中不需要实现任何方法。如果您的类无法实现Serializable
和工作(例如第 3 方库类),此链接可帮助您序列化您的对象。
private void readItems() {
FileInputStream fis = null;
try {
fis = openFileInput("groceries");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<ListItemsModel> list = (ArrayList<ListItemsModel>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
private void writeItems() {
FileOutputStream fos = null;
try {
fos = openFileOutput("groceries", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(itemsList);
} catch (IOException e) {
e.printStackTrace();
}
}