Java如何使用首选项API?这些变量存储在哪里? [英] Java How do you use the preference API? Where do these variables store?

查看:97
本文介绍了Java如何使用首选项API?这些变量存储在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下内容

Preferences prefs = Preferences.userRoot().node(this.getClass().getName());

String ID1 = "Test1";

System.out.println(prefs.getBoolean(ID1, true));

prefs.putBoolean(ID1, false);

//prefs.remove(ID1);




  1. 下次执行程序时,此变量是否持续存在?

  2. 这些变量存储在哪里?

  3. 使用它的正确方法是什么?

  4. 方法是否优于使用属性文件?

  1. Is this variable persistent the next time I execute my program?
  2. Where do these variables store?
  3. What is the proper way of utilizing this?
  4. Is the approach better than using properties files?


推荐答案


  1. 是的,该值是持久的,但仅适用于用户。它不适用于其他用户。

  2. 这是特定于操作系统的。对于Windows,它使用注册表,对于Linux我相信它使用用户root中的隐藏文件,虽然我不是百分百肯定。

  3. 你有一个很好的例子在你的问题。

  4. 不同,而不是更好。首选项是一种透明地存储应用程序设置的方法。用户可以在运行时更新这些设置(例如,您可以使用prefs来存储用户特定的设置)。首选项不应在应用程序之外进行编辑。属性文件倾向于存储特定于应用程序的硬设置。这些设置对于每个用户都是相同的,并且不会经常更改。属性文件是文本文件,在部署时往往伴随应用程序。您可以使用文本编辑器轻松编辑它们。应用程序更新属性文件的情况相当罕见。

  1. Yes, the value is persistent but only for the user. It won't be there for other users.
  2. This is OS specific. For Windows it uses the registry, for Linux I believe it uses hidden files in the user root, although I'm not 100% sure.
  3. You've got a pretty good example in your question.
  4. It is different, not better. Preferences are a way of transparently storing settings for an application. These settings may be updated in run time by a user (for example you could use prefs to store user specific settings). Preferences are not meant to be editable outside of the application. Properties files tend to store hard setting specific to an application. These settings are the same for each user and tend not to change often. Properties files are text files and tend to accompany an application on deployment. You can edit them easily using a text editor. It is fairly rare for an application to update properties files.

这篇关于Java如何使用首选项API?这些变量存储在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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