Exporting, Importing preferences and Getting, Setting preferences values


Exporting preferences

Preferences nodes can be exported into a XML document representing that node. The resulting XML tree can be imported again. The resulting XML document will remember whether it was exported from the user or system Preferences.

To export a single node, but not its child nodes:

// Version ≥ Java SE 7
try (OutputStream outputStream = ...) {
    preferences.exportNode(outputStream);
} catch (IOException ioException) {
    // Exception whilst writing data to the OutputStream
    ioException.printStackTrace();
} catch (BackingStoreException backingStoreException) {
    // Exception whilst reading from the backing preferences store
    backingStoreException.printStackTrace();
}
 
// Version < Java SE 7
OutputStream outputStream = null;
try {
    outputStream = ...;
    preferences.exportSubtree(outputStream);
} catch (IOException ioException) {
    // Exception whilst writing data to the OutputStream
    ioException.printStackTrace();
} catch (BackingStoreException backingStoreException) {
    // Exception whilst reading from the backing preferences store
    backingStoreException.printStackTrace();
} finally {
    if (outputStream != null) {
        try {
            outputStream.close();
        } catch (IOException ignored) {}
    }
}

To export a single node with its child nodes:

// Version ≥ Java SE 7
try (OutputStream outputStream = ...) {
    preferences.exportNode(outputStream);
} catch (IOException writeException) {
    // Exception whilst writing data to the OutputStream
    writeException.printStackTrace();
} catch (BackingStoreException readException) {
    // Exception whilst reading from the backing preferences store
    readException.printStackTrace();
}
 
// Version < Java SE 7
OutputStream outputStream = null;
try {
    outputStream = ...;
    preferences.exportSubtree(outputStream);
} catch (IOException writeException) {
    // Exception whilst writing data to the OutputStream
    writeException.printStackTrace();
} catch (BackingStoreException readException) {
    // Exception whilst reading from the backing preferences store
    readException.printStackTrace();
} finally {
    if (outputStream != null) {
        try {
            outputStream.close();
        } catch (IOException ignored) {}
    }
}

Importing preferences

Preferences nodes can be imported from a XML document. Importing is meant to be used in conjunction with the exporting functionality of Preferences, since it creates the correct corresponding XML documents.

The XML documents will remember whether they were exported from the user or system Preferences. Therefore, they can be imported into their respective Preferences trees again, without you having to figure out or know where they came from. The static function will automatically find out whether the XML document was exported from the user or system Preferences and will automatically import them into the tree they were exported from.

// Version ≥ Java SE 7
try (InputStream inputStream = ...) {
    // This is a static call on the Preferences class
    Preferences.importPreferences(inputStream);
} catch (IOException readException) {
    // Exception whilst reading data from the InputStream
    readException.printStackTrace();
} catch (InvalidPreferencesFormatException parseException) {
    // Exception whilst parsing the XML document tree
    parseException.printStackTrace();
}
 
// Version < Java SE 7
InputStream inputStream = null;
try {
    inputStream = ...;
    // This is a static call on the Preferences class
    Preferences.importPreferences(inputStream);
} catch (IOException readException) {
    // Exception whilst reading data from the InputStream
    readException.printStackTrace();
} catch (InvalidPreferencesFormatException parseException) {
    // Exception whilst parsing the XML document tree
    parseException.printStackTrace();
} finally {
    if (inputStream != null) {
        try {
            inputStream.close();
        } catch (IOException ignored) {}
    }
}

Removing event listeners

Event listeners can be removed again from any Properties node, but the instance of the listener has to be kept around for that.

// Version ≥ Java SE 8
Preferences userPreferences = Preferences.userNodeForPackage(getClass());
PreferenceChangeListener changeListener = evt -> {
    System.out.println(evt.getKey() + " received new value: " + evt.getNewValue());
};
userPreferences.addPreferenceChangeListener(changeListener);
 
//
// later...
//
userPreferences.removePreferenceChangeListener(changeListener);
 
// Version < Java SE 8
Preferences legacyUserPreferences = Preferences.userNodeForPackage(getClass());
PreferenceChangeListener legacyListener = new PreferenceChangeListener() {
    @Override
    public void preferenceChange(PreferenceChangeEvent evt) {
        System.out.println(evt.getKey() + " got updated value: " + evt.getNewValue());
    }
};
legacyUserPreferences.addPreferenceChangeListener(legacyListener);
 
//
// later...
//
legacyUserPreferences.removePreferenceChangeListener(legacyListener);

Getting preferences values

A value of a Preferences node can be of the type String, boolean, byte[], double, float, int or long. All invocations must provide a default value, in case the specified value is not present in the Preferences node.

Preferences userPreferences = Preferences.userNodeForPackage(getClass());
String stringValue = userPreferences.get("differentKey", "this is a new default value");
boolean booleanValue = userPreferences.getBoolean("differentKey", false);
byte[] byteArrayValue = userPreferences.getByteArray("differentKey", new byte[8]);
double doubleValue = userPreferences.getDouble("differentKey", 123456.789d);
float floatValue = userPreferences.getFloat("differentKey", 9876.543f);
int intValue = userPreferences.getInt("differentKey", 654321);
long longValue = userPreferences.getLong("differentKey", 9876543210123L);

Setting preferences values

To store a value into the Preferences node, one of the putXXX() methods is used. A value of a Preferences node can be of the type String, boolean, byte[], double, float, int or long

Preferences userPreferences = Preferences.userNodeForPackage(getClass());
userPreferences.put("differentKey", "a different String value");
userPreferences.putBoolean("differentKey", true);
userPreferences.putByteArray("differentKey", new byte[]{1, 2, 3});
userPreferences.putDouble("differentKey", 987654.321d);
userPreferences.putFloat("differentKey", 1234.567f);
userPreferences.putInt("differentKey", 98765);
userPreferences.putLong("differentKey", 1234567890123L);

Basic Programs