The Chinese meaning of Preferences means preference or preference. That is to say, after each run of the same program, the user's preferences can be recorded through Preferences. The next time the program starts, the program will use this information to understand the user's preferences. And personal understanding of this information should be stored in the system's registry.
Let’s learn the Preferences API in Java; Overview:
This article will introduce Java Preferences available since jdk1.4. Java Preferences API provides system methods to handle user and system preferences and data configuration, such as saving user settings, remembering the last value of a text box, etc. The information saved with Java Preference is stored on the user's local machine, and this information will be reused by this program.
We do not want the Java Preferences API to save application data.
The java Preference API relieves 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 process user preference information, such as saving user settings, remembering the last value of a text box, etc.
Preferences is a key/value pair that can be of any name. The value can be Boolean, character, and other simple data types, such as int. Preferences obtains and sets preference information through get and set, and the get method can set a default value. When the key to be obtained is not set, it returns this default value.
1.2. The actual storage of data The actual storage of data depends on the operating system platform, for example. Under Windows, the registry is used to save this information, and under Linux, it is used to store a hidden file under the user's home directory.
2. The API is easy to use with java.util.prefs.Preferences. You have to define a node to store data. Next, you 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 will be obtained, for example. If the value of preference has not been set yet, this default value will be returned.
The code is created 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(); } }The above is the API for setting user preferences in Java introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!