How do I read a value from the registry of a computer?

John Zukowski

Prior to the 1.4 release, there is no built in support of this, short of using native code. With the 1.4 release, you can do this.

First get the root node for either the system or user:

// Fetching a Preferences object from non-static method
Preferences userPrefs = Preferences.userNodeForPackage(this);
Preferences sysPrefs = Preferences.systemNodeForPackage(this);
// Fetching Preferences object from static method
Preferences userPrefs = Preferences.userRoot().node("/net/zukowski");
Preferences sysPrefs = Preferences.systemRoot().node("/net/zukowski");
Then read from it with one of the following:
  • get(String key, String default)
  • getBoolean(String key, boolean default)
  • getByteArray(String key, byte default[])
  • getDouble(String key, double default)
  • getFloat(String key, float default)
  • getInt(String key, int default)
  • getLong(String key, long default)

If you're not sure of the preference names, you can find a list of the keys associated with a node with the keys() method. This method returns a String[] of nodes.