The methods for reading and writing the Registry in Java: Preferences and jRegistry,

Source: Internet
Author: User

The methods for reading and writing the Registry in Java: Preferences and jRegistry,

This article focuses on the two methods of reading and writing the Registry in Java: Preferences and jRegistry. The details are as follows.

Since the java program is "write once, run everywhere", using java to read and write the registry, the cross-platform nature of the program will be poor. Java operations on the registry are not possible in versions earlier than jdk1.4 and can only be implemented using JNI. However, the prefs package provided after jdk1.4 can be used to operate the windows registry, however, it is fixed that root is only in the SOFTWARE/kerberoft/prefs environment. It is probably out of this dilemma. It is also necessary to ensure that the so-called platform is irrelevant and take care of your reliance on windows. The following describes how to operate the registry from two aspects.

1. Use the Preferences class provided by JDK

First, get an object of Preferences, which specifies the location in the registry where you want to write information, that is, the node. then use put (String key, String value), putInt (), tDouble ()... And so on. The following is a Demo program.

Import java. util. prefs. *; public class Registery {String [] keys = {"version", "initial", "creator"}; String [] values = {"1.3", "ini.mp3 ", "caokai1818@sina.com"}; // Save the corresponding value to the variable to write the registry value under public void writeValue () {// HKEY_LOCAL_MACHINE \ Software \ foreign oft \ prefs. preferences pre = Preferences. systemRoot (). node ("/javaplayer"); for (int I = 0; I <keys. length; I ++) {pre. put (keys, values) ;}} public static void main (String [] args) {Registery reg = new Registery (); reg. writeValue ();}}

Execute the above Code and write the relevant value under the HKEY_LOCAL_MACHINE \ Software \ foreign oft \ prefs \ javaplayer in the registry.

Finally, I will explain the following points:

  • Do not use uppercase letters for your node. Otherwise, a "/" is added before the entry in the registry.
  • The values in the Registry can also be imported into an XML file. For details, see the relevant documentation.
  • If you set Preferences pre = Preferences in the code. systemRoot (). node ("/javaplayer"); Replace with Preferences pre = Preferences. userRoot (). node ("/javaplayer"); then the corresponding HKEY_LOCAL_MACHINE becomes HKEY_LOCAL_USER.
2. Use jRegistry to operate the Registry

JRegistry uses JNI to encapsulate WINDOWS Registry APIs, facilitating java developers to Access windows registry. First, we will introduce jRegistryKey. jar and jRegistryKey. dll, these two files are required to use jRegistry to operate the Registry: A jar package, a file including the java class; a dynamic link library file, provides the local code (C/C ++) required to access the registry ).

The following describes the procedure in detail:

  • Add jRegistryKey. jar to the JBuilder menu Project> Project Properties> Required Libraries or add the jar file to the environment variable classpath;
  • Place jRegistryKey. dll in the current directory of the project;
  • Import this statement in the access registry class: import ca. beq. util. win32.registry. *; there are two classes in this package: RegistryKey and RegistryValue. RegistryKey is the java representation of the registry key. It provides the creat () and delete () methods to create and delete keys, Enumerate sub-keys and values, and set and get key values; registryValue is the Java? Representation of a registry value (defined as a name, a type, and data ).

Implementation Code

  • Create a new key:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\BEQ Technologies"); r.create(); 
  • Create a subkey:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software"); r.createSubkey("BEQ Technologies"); 
  • Delete an existing key value:
try {   RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");   r.delete(); } // try catch(RegistryException re) {   re.printStackTrace(); } // catch 
  • Enumeration subkeys:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software"); if(r.hasSubkeys()) {   Iterator i = r.subkeys();   while(i.hasNext()) {    RegistryKey x = (RegistryKey)i.next();    System.out.println(x.toString());   } // while } // if 
  • Read the key value in the registry:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies"); if(r.hasValue("myValue")) {   RegistryValue v = r.getValue("myValue");   System.out.println(v.toString());// } // if 

Note: v. toString () is only the key value corresponding to the key myValue. To obtain the value data corresponding to the key myValue, String str = v. getDate (). toSting ();

  • Set the key value in the registry:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies"); RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data"); r.setValue(v); 
  • Enumerate all values of a key:
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software"); if(r.hasValues()) {   Iterator i = r.values();   while(i.hasNext()) {    RegistryValue v = (RegistryValue)i.next();    System.out.println(v.toString());   } // while } // if 

The following is a demo program for your reference only.

// create a new key, "Test", under HKLM RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "Test"); if(!r.exists()) { r.create(); } // if  // create value entries RegistryValue v = new RegistryValue("aString", ValueType.REG_SZ, "test"); r.setValue(v); v.setName("aDword"); v.setType(ValueType.REG_DWORD); v.setData(new Integer(0x1001001)); r.setValue(v); // read value entries Iterator i = r.values(); while(i.hasNext()) { v = (RegistryValue)i.next(); System.out.println(v.toString()); } // while // delete registry key r.delete(); 

Summary

The above is all about the methods Preferences and jRegistry for reading and writing the Registry in Java. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.