简体   繁体   English

Java Properties与Android SharedPreferences

[英]Java Properties vs Android SharedPreferences

The goal seems to be the same: to store key-value pairs. 目标似乎是相同的:存储键值对。 Is there a difference in concept between one and the other? 一个与另一个的概念有区别吗?

As far as I can tell, Properties is a HashTable that lets you store key-value pairs on memory with the option of persisting them in XML(and other formats). 据我所知, Properties是一个HashTable ,它允许您将键值对存储在内存中,并可选择将它们保存为XML(和其他格式)。 I do not know where this XML would be stored in Android; 我不知道这个XML在Android中的存储位置; you probably need to define a location. 你可能需要定义一个位置。

SharedPreferences on the other hand, are guaranteed to be stored on disk space that only your app can access. 另一方面, SharedPreferences保证存储在只有您的应用可以访问的磁盘空间中。

From the docs : 来自文档

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. SharedPreferences类提供了一个通用框架,允许您保存和检索原始数据类型的持久键值对。 You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 您可以使用SharedPreferences保存任何原始数据:布尔值,浮点数,整数,长整数和字符串。 This data will persist across user sessions (even if your application is killed). 此数据将在用户会话中持续存在(即使您的应用程序被终止)。

If you need your key-value pairs to be persisted and you don't want to worry about implementations, I would prefer SharedPreferences. 如果您需要持久保存键值对,并且您不想担心实现,我更喜欢SharedPreferences。

In the case of a properties file you will new to write code to store the modified properties. 对于属性文件,您将新编写代码来存储修改后的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM