Jump to Real's How-to Main page

Use the Registry to store informations (Preferences API)

With JDK1.4, the Preferences class can use the Windows registry (on Unix, a file is used) :
import java.util.prefs.Preferences;

public class UsingReg {

public static final String REALKEY= "com.rgagnon.foo";

public static void main(String[] args){
  new UsingReg().doit();
}

public void doit() {
  // write into HKCU\Software\Javasoft\Prefs\com.rgagnon.foo
  Preferences p = Preferences.userRoot();
  p.put(REALKEY, "bar");

  // read back from HKEY_CURRENT_USER
  System.out.println(p);
  System.out.println(p.get(REALKEY, "HKCU houston we have a problem"));

  // write into HKLM\Software\Javasoft\Prefs\com.rgagnon.foo
  p = Preferences.systemRoot();
  p.put(REALKEY, "barbar");

  // read back from HKEY_LOCAL_MACHINE
  System.out.println(p);
  System.out.println(p.get(REALKEY, "HKLM houston we have a problem"));
  }
}

See also this How-to to access the Windows registry.

With Unix (or Linux), a file is used and you may run into problems if you don't have write access in the default location for the Preferences storage. See this interesting article and these 2 bugs :  1      2    


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2005
[ home ]