`

java.util.prefs.Preferences 简介

    博客分类:
  • J2SE
阅读更多

Version 0.2

 

16.10.2004

Abstract

This article gives a introduction into the Java Preference API which is available since Java 1.4. The Java Preferences API provides a systematic way to handle user and system preference and configuration data, e.g. to save user settings, remember the last value of a field etc. Information saves with the Java Preference API is stored on the local machine of the user and is available to be re-used in the program, e.g. after a restart of the program.

This Java Preferences API is not indented to save application data.

The Java Preference API removes the burden from the individual programmer to write code to save configuration values on the different platforms his program may be running.


1. Java Preferences API

1.1. Overview

The Preferences API provides a systematic way to handle program preference configurations, e.g. to save user settings, remember the last value of a field etc.

Preferences are key / values pairs where the key is an arbitrary name for the preference. The value can be a boolean, string, int of another primitive type. Preferences are received and saved by get and put methods while the get methods also supply a default value in case the preferences is not yet set.

1.2. Actual storage of the data

The actual storage of the data is dependent on the platform, e.g. under Windows the Windows Registry is used while under Linux a hidden file in the home directory of the user is used.

2. Using the API

java.util.prefs.Preferences can be easily used. You have to define a node in which the data is stored. Then you can call the getter and setter methods. The second value is the default value, e.g. if the preference value is not set yet, then this value will be used.

Create the following program.

 

			
import java.util.prefs.Preferences;

public class PreferenceTest {
	private Preferences prefs;

	public void setPreference() {
		// This will define a node in which the preferences can be stored
		prefs = Preferences.userRoot().node(this.getClass().getName());
		String ID1 = "Test1";
		String ID2 = "Test2";
		String ID3 = "Test3";

		// First we will get the values
		// Define a boolean value
		System.out.println(prefs.getBoolean(ID1, true));
		// Define a string with default "Hello World
		System.out.println(prefs.get(ID2, "Hello World"));
		// Define a integer with default 50
		System.out.println(prefs.getInt(ID3, 50));

		// Now set the values
		prefs.putBoolean(ID1, false);
		prefs.put(ID2, "Hello Europa");
		prefs.putInt(ID3, 45);

		// Delete the preference settings for the first value
		prefs.remove(ID1);

	}

	public static void main(String[] args) {
		PreferenceTest test = new PreferenceTest();
		test.setPreference();
	}
}

		

 

Run the program twice. The value of "ID1" should be still true as we delete it. The value of "ID2" and "ID2" should have changed after the first call.

3. Thank you

Thank you for practicing with this tutorial.

 

Please note that I maintain this website in my private time. If you like the information I'm providing please help me by donating.

 


4. Questions and Discussion

For questions and discussion around this article please use the www.vogella.de Google Group . Also if you note an error in this article please post the error and if possible the correction to the Group.

 

Tip

分享到:
评论

相关推荐

    使用java修改windows注册表.pdf

    Preferences sysPrefs = Preferences.systemRoot(); // 设置键值 sysPrefs.put("exampleKey", "exampleValue"); // 读取键值 String value = sysPrefs.get("exampleKey", "defaultValue"); System.out....

    JavaSE-6.0-英文手册(2008/11/30_FullUpdate)

    JDKTM 6 Documentation Legal Notices API, Language, and ...org.omg.stub.java.rmi org.w3c.dom org.w3c.dom.bootstrap org.w3c.dom.events org.w3c.dom.ls org.xml.sax org.xml.sax.ext org.xml.sax.helpers

    java操作注册表

    答案是肯定的,从 JDK 1.4 起,Java 提供了 java.util.prefs 包,其中有一个 Preferences 类,可以帮助我们实现对系统注册表进行操作。Preferences 类在不同的平台中有不同的实现方式。在 Windows 平台中,...

    java注册表读写

    对于Java程序来说,虽然不直接访问系统的注册表,但可以通过Java平台提供的`java.util.prefs.Preferences`类来模拟这一功能。这种方式不仅限于Windows系统,也适用于其他操作系统,如Linux或Mac OS。 #### 三、...

    使用java修改windows注册表.doc

    - `java.util.prefs.Preferences` 是Java内置API,用于存储应用的首选项。 - 通过`Preferences`,可以将程序相关的配置信息保存到Windows注册表中。 **步骤**: 1. **创建Preference对象**:通过调用`Preferences....

    Java操作windows注册表

    例如,可以通过调用`Preferences.systemRoot().node("Software\\JavaSoft\\Prefs")`获取到`HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs`节点,然后使用`put`和`get`方法进行数据的存取。 但是,需要注意的是,`...

    Java利用Preferences设置个人偏好,转载自:燕窝

    Preferences API主要包括两个主要类:`java.util.prefs.Preferences.systemNodeForPackage()`和`java.util.prefs.Preferences.userNodeForPackage()`. `systemNodeForPackage()`返回的是系统级别的偏好设置,通常...

    java下操作注册表方法优质资料.doc

    Java 操作注册表主要涉及到 `java.util.prefs` 包中的 `Preferences` 类,这是一个用于存储用户和系统首选项的接口。从 JDK 1.4 开始,Java 提供了这个包来支持跨平台的首选项存储,允许开发者在不依赖特定操作系统...

    java注册表修改

    在Java中,通过`java.util.prefs.Preferences`类可以实现与注册表的交互,包括读取、写入和删除键值等操作。 ### 代码示例解析 在给定的部分内容中,我们看到了一个名为`PrefsDemo`的Java类,该类展示了如何使用...

    使用java修改windows注册表实用.pdf

    本文介绍了三种使用Java修改Windows注册表的方法:利用`java.util.prefs.Preferences`、使用“regedit /s”命令导入注册表文件以及使用第三方库GDAPI。每种方法都各有优势和局限性,开发者应根据实际需求选择最合适...

    Java中使用Preferences 的 API设置用户偏好

    prefs = Preferences.userRoot().node(this.getClass().getName()); String ID1 = "Test1"; String ID2 = "Test2"; String ID3 = "Test3"; // 获取并打印偏好值 System.out.println(prefs.getBoolean(ID1, ...

    修改注册表

    在JDK1.4中,Java平台引入了`java.util.prefs`包,这是通过JSR10(Java Specification Request)规范添加的新功能。这个包提供了一个跨平台的API,允许开发人员在不同的操作系统上操作用户的首选项数据和配置信息。...

    java浏览器

    Java提供了多种持久化存储方案,如`java.util.prefs.Preferences`用于轻量级配置存储,或者使用数据库(如SQLite)进行更复杂的数据管理。 10. **调试和日志**: 开发过程中,调试和日志记录是非常重要的。Java提供...

    java读取和保存property文件(可含中文)

    此外,还可以使用第三方库如Apache Commons Configuration或Java 8引入的`java.util.prefs.Preferences` API来提供更高级的配置管理功能,如支持XML配置、默认值、监听属性变化等。 总结,Java中的`java.util....

    Java Preferences User Interface-开源

    Java Preferences User Interface(JPUI)是一个开源项目,专注于提供一个基于Java/Swing的图形用户界面,使得用户可以方便地浏览和编辑使用Java 1.4.x版本中的`java.util.prefs`包创建的Java首选项。Java首选项系统...

    实现自动登陆(java) 实现自动登陆(java)

    Java提供了多种本地存储选项,如`java.util.prefs.Preferences`系统,或者使用`java.io.File`或`java.nio.file`包来读写文件。 总的来说,实现Java自动登录功能需要理解HTTP协议、Cookie和Session机制,掌握网络...

    用java实现的资源管理器

    10. **持久化状态**:如果需要保存用户自定义的视图设置或最近访问的路径,可以使用`java.util.prefs.Preferences`来存储这些信息。 11. **权限管理**:理解并应用Java的权限模型,确保用户只能执行他们有权限的...

    浅谈Java读写注册表的方式Preferences与jRegistry

    Preferences prefs = Preferences.systemRoot().node("/javaplayer"); prefs.put("version", "1.3"); prefs.put("initial", "ini.mp3"); prefs.put("creator", "***"); // 可以选择性地调用flush()和sync()方法...

    Java licese验证

    import java.util.prefs.Preferences; public class SWLicenseManager { public SWLicenseManager() { } public LicenseContent verifyLicenseKey(LicenseParam parameter) { LicenseManager manager = new ...

Global site tag (gtag.js) - Google Analytics