java.util.prefs.Preferences 简介

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值