Java by API/java.util.prefs/Preferences — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:15, 31 мая 2010

Preferences: absolutePath()

   
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());
  }
}





Preferences: addPreferenceChangeListener(PreferenceChangeListener pcl)

   
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);
    }
  }
}





Preferences: exportNode(OutputStream os)

 
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"));
  }
}





Preferences: exportSubtree(OutputStream os)

   
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();
  }
}





Preferences: getBoolean(String key, boolean def)

 
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[]
  }
}





Preferences: getByteArray(String key, byte[] def)

 
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[]
  }
}





Preferences: getDouble(String key, double def)

 
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[]
  }
}





Preferences: getFloat(String key, float def)

 
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[]
  }
}





Preferences: getInt(String key, int def)

 
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[]
  }
}





Preferences: getLong(String key, long def)

 

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[]
  }
}





Preferences: get(String key, String def)

   
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");
  }
}





Preferences: importPreferences(InputStream is)

  
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);
  }
}





Preferences: keys()

   
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 + "");
    }
  }
}





Preferences.MAX_KEY_LENGTH

 
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;
  }
}





Preferences.MAX_VALUE_LENGTH

 
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;
  }
}





Preferences: name()

   

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);
  }
}





Preferences: nodeExists(String pathName)

 
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"); 
  }
}





Preferences: node(String pathName)

   
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());
  }
}





Preferences: parent()

   
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);
  }
}





Preferences: putBoolean(String key, boolean value)

 
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[]
  }
}





Preferences: putByteArray(String key, byte[] value)

 
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[]
  }
}





Preferences: putDouble(String key, double value)

 
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[]
  }
}





Preferences: putFloat(String key, float value)

 
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[]
  }
}





Preferences: putLong(String key, long value)

 
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[]
  }
}





Preferences: put(String key, String value)

   
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");
  }
}





Preferences: removeNode()

 
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"); 
  }
}





Preferences: remove(String key)

 
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();
  }
}





Preferences: systemRoot()

 

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");
  }
}





Preferences: userNodeForPackage(Class c)

   
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");
  }
}





Preferences: userRoot()

   
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());
  }
}