如何存储在一个文件中的键值对将存储在内部存储器中的Andr​​oid? [英] How to store key-value pairs in a file that will be stored in internal memory in Android?

查看:160
本文介绍了如何存储在一个文件中的键值对将存储在内部存储器中的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要被存储在内部存储器的一些信息。我会喜欢在键 - 值对的共享preferences做的方式的形式来存储。我在想创造的JSONObject和保存和检索以这种方式的。是否有任何其他办法,我们可以保存键 - 值对的形式?

I have some information that needs to be stored in internal memory. I will like to store in the form of key-value pairs the way its done in SharedPreferences. I was thinking of creating JsonObject and saving and retrieving in that way. Is there any other way that we can save the data in internal memory in the form of key-value pairs?

问候
苏尼尔

推荐答案

钍存储对象数据的标准Java方法是使用序列化 - 这是它是有什么。在大多数情况下实现Serializable是相当直截了当。 本网站提供系列化上基本一个很好的教程,它可以针对Android的环境相当容易。

Th standard java way of storing Object data is to use Serialization - that is what it is there for. In Most cases implementing Serializable is fairly straight forward. This Site offers a good tutorial on Serialization basics, which can be tailored to the Android environment quite easily.

通过创建一个HashMap可以序列HashMap的立案在键/值表数据的方便检索。 pretty多所有图书馆馆藏支持序列化,所以你还可以存储其他类型的地图/名单。

By creating a HashMap you can Serialize the HashMap to file for easy retrieval of your data in key/value form. Pretty much all library Collections support Serializable so you can also store other types of Map/List.

所以它使用 - 序列化或JSONObject的?这确实取决于如何重要的效率和多么复杂,你要存储的东西。

So which to use - Serialization or JSONObject? This really depends on how important efficiency is and how complex the things you want to store are.

通常,在两种情况下,最大的成本是将数据写入到文件的实际行为,所以,作为一个JSONObjects几乎总是变小和更简单的,它们可以是快一点。这是罚款用于存储字符串或可转向JSON容易,如果你有几十个成员,许多可能的状态非常复杂的类很简单对象,可能会更容易和有效地使用序列化。在大多数情况下,这些选项之间的实际成本差异可以忽略不计,只会真正的问题,如果你有到preform读写很多很多次。

Generally the largest cost in either case is the actual act of writing data to file, so, as JSONObjects will nearly always be smaller and simpler, they may be a little quicker. This is fine for storing Strings or very Simple Objects that can be turned to JSON easily, If you have a very complex class with dozens of members, and many possible states, it may be simpler and as efficient to use Serialization. The actual cost difference between these options in most cases will be negligible and would only really matter if you had to preform reads and writes many many times.

因此​​,使用JSON的基本类型和序列化复杂类型(你甚至可以存储序列化对象的JSON对象!)

So use JSON for basic types and serialization for complex types (you can even store a serialized object in a JSON Object!)

这篇关于如何存储在一个文件中的键值对将存储在内部存储器中的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆