Getting and Setting Java Type Values in a Preference - Java Native OS

Java examples for Native OS:Preference

Description

Getting and Setting Java Type Values in a Preference

Demo Code

import java.util.prefs.Preferences;

public class Main {

  public void m() {
    Preferences prefs = Preferences.userNodeForPackage(Main.class);
    // Preference key name
    final String PREF_NAME = "name_of_preference";

    // Save//from   w ww .  ja  va  2  s .  co  m
    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[]
  }
}

Related Tutorials