Java by API/java.util.prefs/Preferences
Содержание
- 1 Preferences: absolutePath()
- 2 Preferences: addPreferenceChangeListener(PreferenceChangeListener pcl)
- 3 Preferences: exportNode(OutputStream os)
- 4 Preferences: exportSubtree(OutputStream os)
- 5 Preferences: getBoolean(String key, boolean def)
- 6 Preferences: getByteArray(String key, byte[] def)
- 7 Preferences: getDouble(String key, double def)
- 8 Preferences: getFloat(String key, float def)
- 9 Preferences: getInt(String key, int def)
- 10 Preferences: getLong(String key, long def)
- 11 Preferences: get(String key, String def)
- 12 Preferences: importPreferences(InputStream is)
- 13 Preferences: keys()
- 14 Preferences.MAX_KEY_LENGTH
- 15 Preferences.MAX_VALUE_LENGTH
- 16 Preferences: name()
- 17 Preferences: nodeExists(String pathName)
- 18 Preferences: node(String pathName)
- 19 Preferences: parent()
- 20 Preferences: putBoolean(String key, boolean value)
- 21 Preferences: putByteArray(String key, byte[] value)
- 22 Preferences: putDouble(String key, double value)
- 23 Preferences: putFloat(String key, float value)
- 24 Preferences: putLong(String key, long value)
- 25 Preferences: put(String key, String value)
- 26 Preferences: removeNode()
- 27 Preferences: remove(String key)
- 28 Preferences: systemRoot()
- 29 Preferences: userNodeForPackage(Class c)
- 30 Preferences: userRoot()
Preferences: absolutePath()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.println("Node"s absolute path: " + myPrefs.absolutePath()); }
}
</source>
Preferences: addPreferenceChangeListener(PreferenceChangeListener pcl)
<source lang="java">
import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; public class Main implements PreferenceChangeListener {
private Preferences userPrefs; public static final String NAMEPREF = "name"; public static final String EMAILPREF = "email"; public static void main(String args[]) { new Main(); } public Main() { userPrefs = Preferences.userNodeForPackage(Main.class); System.out.println(userPrefs.get(NAMEPREF, "")); System.out.println(userPrefs.get(EMAILPREF, "")); userPrefs.put(NAMEPREF, "name"); userPrefs.put(EMAILPREF, "email"); Preferences.userNodeForPackage(Main.class).addPreferenceChangeListener(this); } public void preferenceChange(PreferenceChangeEvent evt) { String key = evt.getKey(); String val = evt.getNewValue(); if (key.equals(NAMEPREF)) { System.out.println(val); } else if (key.equals(EMAILPREF)) { System.out.println(val); } }
}
</source>
Preferences: exportNode(OutputStream os)
<source lang="java">
import java.io.FileOutputStream; import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(String.class); // Save some values prefs.put("myString", "a string"); // String prefs.putBoolean("myBoolean", true); // boolean prefs.putInt("myInt", 123); // int prefs.putLong("myLong", 123L); // long prefs.putFloat("myFloat", 12.3F); // float prefs.putDouble("myDouble", 12.3); // double byte[] bytes = new byte[10]; prefs.putByteArray("myByteArray", bytes); // byte[] // Export the node to a file prefs.exportNode(new FileOutputStream("output.xml")); }
}
</source>
Preferences: exportSubtree(OutputStream os)
<source lang="java">
import java.io.FileOutputStream; import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); FileOutputStream fos = new FileOutputStream("prefs.xml"); myPrefs.exportSubtree(fos); fos.close(); }
}
</source>
Preferences: getBoolean(String key, boolean def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: getByteArray(String key, byte[] def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: getDouble(String key, double def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: getFloat(String key, float def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: getInt(String key, int def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: getLong(String key, long def)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: get(String key, String def)
<source lang="java">
import java.util.prefs.Preferences; public class MainClass {
public static void main(String[] args) { // Setup the Preferences for this application, by class. Preferences prefs = Preferences.userNodeForPackage(MainClass.class); // Retrieve some preferences previously stored, with defaults in case // this is the first run. String text = prefs.get("A", "a"); String display = prefs.get("B", "b"); System.out.println(text); System.out.println(display); // Assume the user chose new preference values: Store them back. prefs.put("A", "aa"); prefs.put("B", "bb"); }
}
</source>
Preferences: importPreferences(InputStream is)
<source lang="java">
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { // Create an input stream on a file InputStream is = new BufferedInputStream(new FileInputStream("output.xml")); // Import preference data Preferences.importPreferences(is); }
}
</source>
Preferences: keys()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.print("Node"s keys: "); for (String s : myPrefs.keys()) { System.out.print(s + ""); } }
}
</source>
Preferences.MAX_KEY_LENGTH
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { // Get maximum key length int keyMax = Preferences.MAX_KEY_LENGTH; // Get maximum value length int valueMax = Preferences.MAX_VALUE_LENGTH; // Get maximum length of byte array values int bytesMax = Preferences.MAX_VALUE_LENGTH * 3 / 4; }
}
</source>
Preferences.MAX_VALUE_LENGTH
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { // Get maximum key length int keyMax = Preferences.MAX_KEY_LENGTH; // Get maximum value length int valueMax = Preferences.MAX_VALUE_LENGTH; // Get maximum length of byte array values int bytesMax = Preferences.MAX_VALUE_LENGTH * 3 / 4; }
}
</source>
Preferences: name()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.println("Node"s name: " + myPrefs.name()); System.out.println("Node"s parent: " + myPrefs.parent()); System.out.println("NODE: " + myPrefs); }
}
</source>
Preferences: nodeExists(String pathName)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { boolean exists = Preferences.userRoot().nodeExists("/yourValue"); if (!exists) { Preferences prefs = Preferences.userRoot().node("/yourValue"); prefs.removeNode(); // prefs.removeNode(); } Preferences prefs = Preferences.userRoot().node("/yourValue/child"); exists = Preferences.userRoot().nodeExists("/yourValue"); exists = Preferences.userRoot().nodeExists("/yourValue/child"); Preferences.userRoot().node("/yourValue").removeNode(); exists = Preferences.userRoot().nodeExists("/yourValue"); exists = Preferences.userRoot().nodeExists("/yourValue/child"); }
}
</source>
Preferences: node(String pathName)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.println("Node"s absolute path: " + myPrefs.absolutePath()); }
}
</source>
Preferences: parent()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.println("Node"s name: " + myPrefs.name()); System.out.println("Node"s parent: " + myPrefs.parent()); System.out.println("NODE: " + myPrefs); }
}
</source>
Preferences: putBoolean(String key, boolean value)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: putByteArray(String key, byte[] value)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: putDouble(String key, double value)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: putFloat(String key, float value)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: putLong(String key, long value)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { Preferences prefs = Preferences.userNodeForPackage(Main.class); // Preference key name final String PREF_NAME = "name_of_preference"; // Save prefs.put(PREF_NAME, "a string"); // String prefs.putBoolean(PREF_NAME, true); // boolean prefs.putInt(PREF_NAME, 123); // int prefs.putLong(PREF_NAME, 123L); // long prefs.putFloat(PREF_NAME, 12.3F); // float prefs.putDouble(PREF_NAME, 12.3); // double byte[] bytes = new byte[1024]; prefs.putByteArray(PREF_NAME, bytes); // byte[] // Retrieve String s = prefs.get(PREF_NAME, "a string"); // String boolean b = prefs.getBoolean(PREF_NAME, true); // boolean int i = prefs.getInt(PREF_NAME, 123); // int long l = prefs.getLong(PREF_NAME, 123L); // long float f = prefs.getFloat(PREF_NAME, 12.3F); // float double d = prefs.getDouble(PREF_NAME, 12.3); // double bytes = prefs.getByteArray(PREF_NAME, bytes); // byte[] }
}
</source>
Preferences: put(String key, String value)
<source lang="java">
import java.util.prefs.Preferences; public class MainClass {
public static void main(String[] args) { // Setup the Preferences for this application, by class. Preferences prefs = Preferences.userNodeForPackage(MainClass.class); // Retrieve some preferences previously stored, with defaults in case // this is the first run. String text = prefs.get("A", "a"); String display = prefs.get("B", "b"); System.out.println(text); System.out.println(display); // Assume the user chose new preference values: Store them back. prefs.put("A", "aa"); prefs.put("B", "bb"); }
}
</source>
Preferences: removeNode()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { boolean exists = Preferences.userRoot().nodeExists("/yourValue"); if (!exists) { Preferences prefs = Preferences.userRoot().node("/yourValue"); prefs.removeNode(); // prefs.removeNode(); } Preferences prefs = Preferences.userRoot().node("/yourValue/child"); exists = Preferences.userRoot().nodeExists("/yourValue"); exists = Preferences.userRoot().nodeExists("/yourValue/child"); Preferences.userRoot().node("/yourValue").removeNode(); exists = Preferences.userRoot().nodeExists("/yourValue"); exists = Preferences.userRoot().nodeExists("/yourValue/child"); }
}
</source>
Preferences: remove(String key)
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { // Get the user preference node for java.lang Preferences prefs = Preferences.userNodeForPackage(String.class); // Remove a preference in the node final String PREF_NAME = "name_of_preference"; prefs.remove(PREF_NAME); // Remove all preferences in the node prefs.clear(); }
}
</source>
Preferences: systemRoot()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String[] argv) throws Exception { // System preference nodes // Use a Class Preferences prefs = Preferences.systemNodeForPackage(java.lang.String.class); // Use an absolute path prefs = Preferences.systemRoot().node("/java/lang/String"); // Use a relative path prefs = Preferences.systemRoot().node("/javax/swing"); prefs = prefs.node("text/html"); // User preference nodes // Use a class prefs = Preferences.userNodeForPackage(Main.class); // Use an absolute path prefs = Preferences.userRoot().node("/com/mycompany"); // Use a relative path prefs = Preferences.userRoot().node("/javax/swing"); prefs = prefs.node("text/html"); }
}
</source>
Preferences: userNodeForPackage(Class c)
<source lang="java">
import java.util.prefs.Preferences; public class MainClass {
public static void main(String[] args) { // Setup the Preferences for this application, by class. Preferences prefs = Preferences.userNodeForPackage(MainClass.class); // Retrieve some preferences previously stored, with defaults in case // this is the first run. String text = prefs.get("A", "a"); String display = prefs.get("B", "b"); System.out.println(text); System.out.println(display); // Assume the user chose new preference values: Store them back. prefs.put("A", "aa"); prefs.put("B", "bb"); }
}
</source>
Preferences: userRoot()
<source lang="java">
import java.util.prefs.Preferences; public class Main {
public static void main(String args[]) throws Exception { Preferences prefsRoot = Preferences.userRoot(); Preferences myPrefs = prefsRoot.node("PreferenceExample"); myPrefs.put("A", "a"); myPrefs.put("B", "b"); myPrefs.put("C", "c"); System.out.println("Node"s absolute path: " + myPrefs.absolutePath()); }
}
</source>