How do I read / write data in Windows registry?
Category: java.util, viewed: 21121 time(s).
The Preferences class allows us to read and write information from the Windows registry. In this example we read and write to the HKCU and HKLM in the registry.
package org.kodejava.example.util;
import java.util.prefs.Preferences;
public class RegistryDemo {
public static final String PREF_KEY = "org.kodejava";
public static void main(String[] args) {
//
// Write Preferences information to HKCU (HKEY_CURRENT_USER),
// HKCU\Software\JavaSoft\Prefs\org.kodejava
//
Preferences userPref = Preferences.userRoot();
userPref.put(PREF_KEY, "www.kodejava.org");
//
// Below we read back the value we've written in the code above.
//
System.out.println("Preferences = "
+ userPref.get(PREF_KEY, PREF_KEY + " was not found."));
//
// Write Preferences information to HKLM (HKEY_LOCAL_MACHINE),
// HKLM\Software\JavaSoft\Prefs\org.kodejava
//
Preferences systemPref = Preferences.systemRoot();
systemPref.put(PREF_KEY, "www.kodejava.org");
//
// Read back the value we've written in the code above.
//
System.out.println("Preferences = "
+ systemPref.get(PREF_KEY, PREF_KEY + " was not found."));
}
}
Can't find what you are looking for? Join our
FORUMS and ask some questions!
Download Hundreds of Complimentary Industry Resources
Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more;
all available at no cost to you. With more than 600 complimentary offers, you'll find
plenty of titles to suit your professional interests and needs.
Click Here and Sign up today!