Java Preference Put put(final String key, final boolean value)

Here you can find the source of put(final String key, final boolean value)

Description

Stores a boolean value to the preferences object

License

Open Source License

Parameter

Parameter Description
key the string that will be used to retrieve the value
value the value to be stored

Declaration

public static void put(final String key, final boolean value) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.prefs.Preferences;

public class Main {
    /**/*from w ww . j  a  v a 2 s  . co  m*/
     * The preferences node to load/save from/to
     */
    private static final Preferences prefs = Preferences.userRoot().node("/edu/colostate/vchill");

    /**
     * Stores a boolean value to the preferences object
     *
     * @param key   the string that will be used to retrieve the value
     * @param value the value to be stored
     */
    public static void put(final String key, final boolean value) {
        prefs.putBoolean(key, value);
    }

    /**
     * Stores a double value to the preferences object
     *
     * @param key   the string that will be used to retrieve the value
     * @param value the value to be stored
     */
    public static void put(final String key, final double value) {
        prefs.putDouble(key, value);
    }

    /**
     * Stores an integer value to the preferences object
     *
     * @param key   the string that will be used to retrieve the value
     * @param value the value to be stored
     */
    public static void put(final String key, final int value) {
        prefs.putInt(key, value);
    }

    /**
     * Stores a string value to the preferences object
     *
     * @param key   the string that will be used to retrieve the value
     * @param value the value to be stored
     */
    public static void put(final String key, final String value) {
        prefs.put(key, value);
    }

    /**
     * Stores a string array value to the preferences object
     *
     * @param key   the string that will be used to retrieve the value
     * @param value the value to be stored (none of the strings in the array may contain "<code>*</code>")
     */
    public static void put(final String key, final String[] value) {
        prefs.put(key, arrayToString(value));
    }

    /**
     * Converts a String[] to a single String
     *
     * @param value the String[] to convert
     * @return the contents of <code>value</code> concatenated, with "<code>*</code>" as separator
     */
    private static String arrayToString(final String[] value) {
        if (value == null || value.length == 0)
            return "";
        StringBuilder buff = new StringBuilder(value[0]);
        for (int i = 1; i < value.length; ++i)
            buff.append("*").append(value[i]);
        return buff.toString();
    }
}

Related

  1. putBooleanDontOverwrite(Preferences prefs, String key, boolean value)
  2. putCLOB(Preferences prefs, String key, String clob)
  3. putList(String path, ArrayList list)
  4. putPrefsBoolean(Preferences prefs, String key, boolean value, boolean def)