How do I write a value to 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 write to it with one of the following:
  • put(String key, String value)
  • putBoolean(String key, boolean value)
  • putByteArray(String key, byte value[])
  • putDouble(String key, double value)
  • putFloat(String key, float value)
  • putInt(String key, int value)
  • putLong(String key, long value)

All the put() methods return a void. If the storage mechanism is unavailable, a BackingStoreException will be thrown.