Example usage for java.util.prefs Preferences keys

List of usage examples for java.util.prefs Preferences keys

Introduction

In this page you can find the example usage for java.util.prefs Preferences keys.

Prototype

public abstract String[] keys() throws BackingStoreException;

Source Link

Document

Returns all of the keys that have an associated value in this preference node.

Usage

From source file:PreferenceExample.java

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

    for (String s : myPrefs.keys()) {
        System.out.println("" + s + "= " + myPrefs.get(s, ""));
    }//w w w.ja v a 2  s. c o  m

}

From source file:PreferenceExample.java

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 + "");
    }//from  w w w.  ja v  a2  s. co m
}

From source file:PlanetPrefs.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    int moons[] = { 0, 0, 1, 2, 16, 18, 21, 8, 1 };

    Preferences prefs = Preferences.userRoot().node("/MasteringJava/Chap17");

    for (int i = 0, n = names.length; i < n; i++) {
        prefs.putInt(names[i], moons[i]);
    }/*w w  w.  j a  v a2 s. c  o  m*/

    try {
        String keys[] = prefs.keys();
        for (int i = 0, n = keys.length; i < n; i++) {
            System.out.println(keys[i] + ": " + prefs.getInt(keys[i], 0));
        }
    } catch (BackingStoreException e) {
        System.err.println("Unable to read backing store: " + e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;//from  w ww.  j  a  v  a2  s .  c  om
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(MainClass.class);
    prefs.put("key1", "value1");
    prefs.put("key2", "value2");
    prefs.putInt("intValue", 4);
    prefs.putBoolean("booleanValue", true);
    int usageCount = prefs.getInt("intValue", 0);
    usageCount++;// w w w  .  j a va2 s . c  om
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    System.out.println(prefs.getInt("booleanValue", 0));
}

From source file:PreferencesDemo.java

public static void main(String[] args) throws Exception {
    Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class);
    prefs.put("Location", "Oz");
    prefs.put("Footwear", "Ruby Slippers");
    prefs.putInt("Companions", 4);
    prefs.putBoolean("Are there witches?", true);
    int usageCount = prefs.getInt("UsageCount", 0);
    usageCount++;/*from w w w  .j a v  a 2s  .c o  m*/
    prefs.putInt("UsageCount", usageCount);
    Iterator it = Arrays.asList(prefs.keys()).iterator();
    while (it.hasNext()) {
        String key = it.next().toString();
        System.out.println(key + ": " + prefs.get(key, null));
    }
    // You must always provide a default value:
    System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions", 0));
}

From source file:Main.java

public static String containsValue(Preferences node, String value) {
    try {/* w  ww.ja  v a2s  .c om*/
        String[] keys = node.keys();

        for (int i = 0; i < keys.length; i++) {
            if (value.equals(node.get(keys[i], null))) {
                return keys[i];
            }
        }
    } catch (BackingStoreException e) {
    }
    return null;
}

From source file:org.apache.cayenne.modeler.ModelerPreferences.java

public static List<String> getLastProjFiles() {
    Preferences filesPrefs = getLastProjFilesPref();
    ArrayList<String> arrayLastProjFiles = new ArrayList<>();
    String[] keys = null;/* w  w w  . j  a  va2s .c o m*/
    try {
        keys = filesPrefs.keys();
    }

    catch (BackingStoreException e) {
        logObj.warn("Error reading preferences file.", e);
    }
    if (keys != null) {
        int len = keys.length;

        ArrayList<Integer> keysInteger = new ArrayList<>();
        for (int i = 0; i < len; i++) {
            keysInteger.add(new Integer(i));
        }
        Collections.sort(keysInteger);

        for (int i = 0; i < len; i++) {
            arrayLastProjFiles.add(filesPrefs.get(keysInteger.get(i).toString(), ""));
        }
    }
    return arrayLastProjFiles;
}

From source file:org.apache.cayenne.modeler.preferences.ModelerPreferences.java

public static List<String> getLastProjFiles() {
    Preferences filesPrefs = getLastProjFilesPref();
    ArrayList<String> arrayLastProjFiles = new ArrayList<String>();
    String[] keys = null;/*w ww. j a  v a2s  . c o m*/
    try {
        keys = filesPrefs.keys();
    }

    catch (BackingStoreException e) {
        logObj.warn("Error reading preferences file.", e);
    }
    if (keys != null) {
        int len = keys.length;

        ArrayList<Integer> keysInteger = new ArrayList<Integer>();
        for (int i = 0; i < len; i++) {
            keysInteger.add(new Integer(i));
        }
        Collections.sort(keysInteger);

        for (int i = 0; i < len; i++) {
            arrayLastProjFiles.add(filesPrefs.get(keysInteger.get(i).toString(), ""));
        }
    }
    return arrayLastProjFiles;
}

From source file:PrefsUtil.java

/**
 * Gets a Map from the preferences./*from   w ww  .  j  av  a 2s  .  co m*/
 */
public static Map getMap(Preferences preferences) {
    if (preferences == null) {
        throw new IllegalArgumentException("Preferences not set.");
    }

    Map map = new HashMap();

    try {
        String[] keys = preferences.keys();

        for (int index = 0; index < keys.length; index++) {
            map.put(keys[index], preferences.get(keys[index], null));
        }
    } catch (BackingStoreException bse) {
        bse.printStackTrace();
    }

    return map;
}