Java uses Preferences to set personal Preferences, javapreferences
Java uses Preferences to set personal Preferences
Preferences are both Preferences and Preferences. In other words, after each program is run, Preferences can be used to record user Preferences, the program will use this information to understand the user's preferences. The personal understanding of this information should be stored in the registry of the system.
Next, let's take a look at the Preferences API in Java. Overview:
This article describes the Preferences of java available after jdk1.4. the Preferences API of Java provides a systematic method to handle user and system Preferences and Data configuration, for example. save User settings and remember the last value of a text box. the information stored with Java Preference is stored on the user's local machine and will be reused by this program.
We do not want Java Preferences APIs to save application data.
Java Preference API reduces the burden on programmers to write code to save configuration information of cross-platform programs.
1. Java Preferences API 1.1. This section describes The Preferences API. It provides a systematic method to process user Preferences, such as saving user settings and remembering The last value of a text box.
Preferences is a key/value pair that can be any name. the value can be boolean or numeric, and other simple data types, such as int. preferences uses get and set to obtain and set Preferences. You can set a default value for the get method. If the key to be obtained is not set, this default value is returned.
1.2. the actual storage of data depends on the operating system platform, for example. in Windows, the Registry is used to save the information, while in Linux, a hidden file under the user's home directory is used to store the information.
2. the API uses java. util. prefs. preferences are easy to use. you have to define a node to store data. next we can use the get and set methods. the second parameter is the default value, that is, when the value cannot be found, the default value is obtained, for example. if the preference value is not set, the default value is returned.
The creation code is as follows:
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(); } }