Java 如何使用首选项 API?这些变量存储在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10246503/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:12:22  来源:igfitidea点击:

Java How do you use the preference API? Where do these variables store?

javapreferences

提问by stackoverflow

Say I have the following

说我有以下

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. 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. 这些变量存储在哪里?
  3. 使用它的正确方法是什么?
  4. 该方法是否比使用属性文件更好?

回答by Qwerky

  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.
  1. 是的,该值是持久的,但仅适用于用户。它不会为其他用户提供。
  2. 这是特定于操作系统的。对于 Windows,它使用注册表,对于 Linux,我相信它在用户根目录中使用隐藏文件,尽管我不是 100% 确定。
  3. 你的问题中有一个很好的例子。
  4. 这是不同的,不是更好。首选项是一种透明地存储应用程序设置的方式。这些设置可能会在运行时由用户更新(例如,您可以使用首选项来存储用户特定的设置)。首选项不能在应用程序之外进行编辑。属性文件倾向于存储特定于应用程序的硬设置。这些设置对于每个用户都是相同的,并且不会经常更改。属性文件是文本文件,往往伴随着部署的应用程序。您可以使用文本编辑器轻松编辑它们。应用程序很少更新属性文件。