Use Preferences APIs in Java to set user preferences _java

Source: Internet
Author: User
Tags id3

Preferences Chinese meaning is the meaning of preference or preference, that is, the same program after each run, you can record the user's preferences by preferences, the next time the program will use this information to understand the user's preferences. And this information is personally understood to be stored in the system's registry.

Let's take a look at the preferences API in Java; Overview:

This article describes the Java preferences. Java Preferences API available from the jdk1.4 version to provide a systematic approach to user and system preferences and data configuration, such as. Save user settings, remember the last value of a text box, and so on. The information saved in Java preference is stored on the user's local machine and will be reused by this program.

We do not want the Java Preferences API to hold the application data.

The Java Preference API eases the burden of programmers writing code to save configuration information for cross-platform programs.

1. Java Preferences API 1.1. Introduction The Preferences API provides a systematic way to handle user preferences information, such as. Save user settings, remember the last value of a text box, and so on.

Preferences is a key/value pair that can be any name. Values can be Boolean, character type, and other simple data types, such as int. Preferences gets and sets preference information through get and set, and the Getting method can set a default value that is returned when the key to get is not set.

1.2. The actual storage of data in actual storage is dependent on the operating system platform, for example. The registry is used to store this information under windows and is stored under Linux by using a hidden file below the user's home directory.

2. API use Java.util.prefs.Preferences is easy to use. You have to define a node to store the data. You can then use the methods of get and set. The second parameter is the default value, which means that when the value is not found, the default value is obtained, for example. If the value of the preference is not set, the default value will be returned.

Create the following code:

 import java.util.prefs.Preferences; 
public class Preferencetest {private Preferences prefs; public void Setpreference () {//This'll define a node in which the preferences can is stored prefs = Preferences.user 
Root (). Node (This.getclass (). GetName ()); 
String ID1 = "Test1"; 
String ID2 = "Test2"; 
String ID3 = "Test3"; 
We'll 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 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 ' The ' the ' the ' the ' I Value prefs.remove (ID1); 
public static void Main (string[] args) {preferencetest test = new Preferencetest (); 
Test.setpreference (); } 
} 

The above is a small set of Java to introduce the use of Preferences API settings user Preferences, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.