我想从这样的结构存储一些数据:
I want to store some data from structures like this:
class Project { ChildA a; ChildB b; } class ChildA { ChildC c; ... } ...我有创建数据,如:
Projet x = new Project(); x.a = new ChildA(); x.a.c = new ChildC();...我想将其保存到设备的SD卡上的外部文件,以便其他应用程序可以读取它(用户可以打开/复制它)。
... and I want to save it to an external file on the sdcard of my device so that other apps can read it (and users can open/copy it).
我以为我应该使用一个DOM解析器,因为我的数据结构不是很大,但我发现没有教程或任何类似的。有没有更好的方式存储这些信息?
I thought I should use a DOM parser because my data structures are not very big, but I find no tutorials or anything like that. Is there a better way to store this information?
如果没有,有什么说明如何使用android中的解析器?
If not, are there any instructions how to use the parsers in android?
推荐答案如果您希望其他Android应用程序可以访问您的数据,那么推荐的方法是写一个内容提供者。另一方面,如果您希望将数据用于非Android应用程序,则需要导出。你必须自己编码。
If you want your data to be accessible to other Android applications, then the recommended way is to write a Content Provider. If, on the other hand you want your data to be used in non-android applications, you will need to "export" it. This you have to code on your own.
我现在可以想到三个选项:
I can think of three options right now:
当您私有存储文件时,请考虑使用 openFileInput 和 openFileOutput 。如果要明确存储在SD卡上,您应该使用 getExternalStorageDirectory()来检索根文件夹。
When you store your files privately, then consider using openFileInput and openFileOutput. If you want to store explicitly on the SD-Card, you should use getExternalStorageDirectory() to retrieve the root folder.
我个人的建议是使用JSON文件。这是一种简单的格式,在大多数情况下都很好,广泛可用。除了这个去/序列化是简单的。
My personal recommendation would be to use JSON files. It's a simple format which does well for most cases and it's widely available. Apart from that de/serialization is dead-simple.
更多推荐
如何在Android中保存数据结构?
发布评论