This article mainly studies the two ways of reading and writing registry in Java Preferences and jRegistry. The details are as follows.
Since the java program is "write once, run everywhere", and using java to read and write the registry, the cross-platform nature of the program is poor. Java's operation of the registry is impossible in versions before jdk1.4, and can only be implemented with JNI; however, the prefs package provided after jdk1.4 can operate the Windows registry, but root is only under SOFTWARE/JavaSoft/prefs. It is probably because of this dilemma, and it is necessary to ensure that the so-called platform is irrelevant and take care of everyone's dependence on Windows. The following will introduce the operation of the registry from two aspects.
First, get an object of Preferences, which specifies where you want to write information in the registry, that is, the node. Then use put(String key,String value) or putInt(), tDouble(), etc. to assign values to the relevant items. Here is the demo program.
import java.util.prefs.*; public class Registery { String[] keys = {"version", "initial", "creator"}; String[] values = {"1.3", "ini.mp3", "[email protected]"}; //Save the corresponding value in a variable and go to public void writeValue() { // Write the registry value under HKEY_LOCAL_MACHINE/Software/JavaSoft/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/JavaSoft/prefs/javaplayer in the registry.
Finally, let me explain a few points:
jRegistry uses JNI to encapsulate the WINDOWS registry API, which facilitates Java developers to access the Windows registry. First, let’s introduce jRegistryKey.jar and jRegistryKey.dll. These two files are necessary to use jRegistry to operate the registry: one is the jar package, which is a file that includes the java class; the other is a dynamic link library file, which provides the local code required to access the registry (i.e., C/C++).
The following is a detailed introduction to the usage process:
Implement code
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software//BEQ Technologies"); r.create();
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software"); r.createSubkey("BEQ Technologies"); try { RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software//BEQ Technologies"); r.delete(); } // try catch(RegistryException re) { re.printStackTrace(); } // catch 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 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());// } // ifNote: v.toString() is only the key value corresponding to the key myValue. To get the value data corresponding to the myValue key, you need String str = v.getDate().toSting();
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software//BEQ Technologies"); RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data"); r.setValue(v); 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 } // ifThe following is a demo program for 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();Summarize
The above is the entire content of this article about briefly discussing the ways of reading and writing registry in Java Preferences and jRegistry. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!