A central hierarchical database used in Microsoft Windows 9x, Windows CE, Windows NT, and Windows 2000 to store information required to configure the system for one or more users, applications, and hardware devices.
A central hierarchical database used in Microsoft Windows 9x, Windows CE, Windows NT, and Windows 2000 to store information required to configure the system for one or more users, applications, and hardware devices.
The registry contains information that Windows continuously references during running, for example, configuration files for each user, applications installed on the computer, document types, folder and application icon Attribute Table settings that can be created by each application, hardware on the system, and the hardware in use ports.
The Registry replaces the vast majority of text-based. ini files used in Windows 3.x and MS-DOS configuration files, such as autoexec. BAT and config. sys. Although several Windows operating systems have registries, the Registries of these operating systems are different.
If you do not know the above content, you may need to read this article Microsoft Windows registry description, which can be found in reference materials, you can also find its original English text "Description of the Microsoft Windows Registry ".
If you understand the above content, you can continue.
Use preferences API
Preferences API is not designed to access the Windows registry, which is worth noting.
The above misunderstanding is that Sun's windows JDK uses the Windows registry as the repository when implementing the preferences API, that is, the data stored using the preferences API is saved to the Windows registry, so that the preferenes API can access the Windows registry. But what if I switched to another platform or another vendor's JDK implementation? This question is related to the implementation of the preferences API and cannot be answered.
If the program does not care about the details of the repository, but wants to find a place to store data, the preferences API is suitable.
The preferences API is also limited. Please consider two issues:
- A Java software. This time I run it on Sun's JDK and use the preferences API to save my personal preferences. Next time I run it on IBM's JDK, can my preferences be obtained through the preferences API? It may or may not. At this time, the behavior is determined by the implementation of the preferences API of sun and IBM. (In the same JDK implementation, you can use the preferences API to share data among different programs)
- A Java software requires users to set whether to start with the operating system. There are still many similar requirements. This requirement requires that Java programs have the ability to truly coordinate with relevant operating systems. This capability is not designed for preferences APIs.
If you do not know the preferences API, you may need to read this article "using preferences API to store objects", which can be found in references.
Back to Top using JNI
The Windows operating system provides APIs for operating the registry. Therefore, using JNI to connect Java with these Apis gives us the ability to operate the registry using Java. This is a bit simple, but a lot of details need to be processed. Fortunately, someone has done this kind of work, and we want to thank them. Next let's take a look at one of the packages.
The com. Ice. JNI. Registry package is a Windows registry operation API implemented by JNI (Java Native Interface). It can be used to access, modify, and export windows registries. Now this package is made public, so you can use it with peace of mind without having to worry about license issues. It also includes the source code of a constructed DLL, Java, and C. It can work in Java 1.1 and later versions.
If you are also interested in JNI, this is also a good example of learning JNI.
The following describes in detail the classes in this package. There are not many classes:
- Hexnumberformat is used to format and analyze hexadecimal integers.
- Regbinaryvalue indicates the registry value of the REG_BINARY type. REG_BINARY refers to the number of binary data in any form.
- Regdwordvalue indicates the registry value of REG_DWORD type. REG_DWORD is a 32-bit integer. Reg_dword_little_endian and reg_dword_big_endian are sorted according to the byte order of the integer. In Windows, REG_DWORD and reg_dword_little_endian share the same meaning.
- Registryvalue indicates any type of registry value. It is an abstract class and cannot be instantiated.
- Regmultistringvalue indicates the registry value of the reg_multi_sz type. Reg_multi_sz is the sequence of a null-terminated string.
- Regstringvalue indicates the registry values of the REG_SZ and reg_expand_sz types. REG_SZ refers to a null-terminated string, and reg_expand_sz refers to a null-terminated string containing unexpanded environment variables.
- The Registry class defines the grading items (key), including hkey_classes_root, hkey_current_config, HKEY_CURRENT_USER, hkey_dyn_data, HKEY_LOCAL_MACHINE, hkey_cece_data, and HKEY_USERS. The error code is also defined, which will be included in registryexception. Finally, there are some tool methods, such as dumphexdata, exportregistrykey, geterrormessage, gettoplevelkey, opensubkey, parseargumentstring, parseargumentvector, splitstring, and usage.
- Registrykey defines a table key in the registry and related operations.
For more details, see jniregistry's Java Doc, registry, and registry reference.
Registrykey method Overview:
Method |
Brief Description |
Void closekey () |
Disable the subkey. |
Registrykey connectregistry (Java. Lang. String hostname) |
Connect to the registry of the remote host hostname. |
Registrykey createsubkey (Java. Lang. String subkey, java. Lang. String classname) |
Create and open the subkey of the key with write permission. |
Registrykey createsubkey (Java. Lang. String subkey, java. Lang. String classname, int access) |
You have the specified permission to create and open the subkey of the key. |
Int decrdoubleword (Java. Lang. String valuename) |
This method will reduce the value of REG_DWORD. |
Void deletesubkey (Java. Lang. String subkey) |
Delete the subkey. |
Void deletevalue (Java. Lang. String valuename) |
Delete A named value. |
Static java. Lang. String expandenvstrings (Java. Lang. String exstring) |
Expand the environment variable in exstring. |
Void Export (Java. Io. printwriter out, Boolean descend) |
Export key. |
Void finalize () |
The overloaded finalize () method ensures that the key can be disabled. |
Void flushkey () |
Make sure that this key is written to the disk, which may affect the performance. |
Java. Lang. String getdefaultvalue () |
Obtain the default value of the key. |
Java. Lang. String getfullname () |
Obtain the full name of the key. |
Int getmaxsubkeylength () |
Obtain the maximum length of all subkey names. |
Int getmaxvaluedatalength () |
Obtain the maximum length of all subkey values. |
Int getmaxvaluenamelength () |
Obtain the maximum length of all values. |
Java. Lang. String getname () |
Obtain the name of the key. |
Int getnumbersubkeys () |
Obtain the number of subkeys. |
Int getnumbervalues () |
The number of values. |
Java. Lang. String getstringvalue (Java. Lang. String valuename) |
Obtain the value of REG_SZ or reg_expand_sz. |
Registryvalue getvalue (Java. Lang. String valuename) |
Obtain the value of valuename. |
Boolean hasdefaultvalue () |
Determines whether the key has a default value. |
Boolean hasonlydefaultvalue () |
Determines whether the key has only the default value. |
Int incrdoubleword (Java. Lang. String valuename) |
This method will enhance the value of REG_DWORD. |
Java. util. Enumeration keyelements () |
Name of the subkey of the key. |
Registrykey opensubkey (Java. Lang. String subkey) |
Open the subkey of the key and have the write permission. |
Registrykey opensubkey (Java. Lang. String subkey, int access) |
Open the subkey of the key with the specified permission. |
Java. Lang. String regenumkey (INT index) |
Obtain the subkey of the key at the index. |
Java. Lang. String regenumvalue (INT index) |
Obtain the value of the subkey at the index. |
Void setcreated (Boolean created) |
Set the created status of the key. |
Void setvalue (registryvalue value) |
Set the value of the key. |
Void setvalue (Java. Lang. String valuename, registryvalue value) |
Set the value of valuename. |
Java. util. Enumeration valueelements () |
Name of the value of the key. |
Boolean wascreated () |
Determine whether the key is opened, created, or opened. |
Finally, let's look at a sample code:
Package Org. solol. test; import COM. ice. JNI. registry. nosuchkeyexception; import COM. ice. JNI. registry. regstringvalue; import COM. ice. JNI. registry. registry; import COM. ice. JNI. registry. registryexception; import COM. ice. JNI. registry. registrykey;/*** @ author solo l ***/public class jniregistrytest {/*** @ Param ARGs */public static void main (string [] ARGs) {// create a registry key and set the value try {registrykey software = registry. HKEY_LOCAL_MACHINE. opensubkey ("software"); registrykey subkey = software. createsubkey ("subkeyname", ""); subkey. setvalue (New regstringvalue (subkey, "subkey1", "subkey1value"); subkey. setvalue (New regstringvalue (subkey, "subkey2", "subkey2value"); subkey. closekey ();} catch (nosuchkeyexception e) {e. printstacktrace ();} catch (registryexception e) {e. printstacktrace ();} // open the registry key and read the corresponding value. Try {registrykey software = registry. HKEY_LOCAL_MACHINE. opensubkey ("software"); registrykey subkey = software. opensubkey ("subkeyname"); string subkey1value = subkey. getstringvalue ("subkey1"); string subkey2value = subkey. getstringvalue ("subkey2"); system. out. println (subkey1value); system. out. println (subkey2value); subkey. closekey ();} catch (nosuchkeyexception e) {e. printstacktrace ();} catch (registryexception e) {e. printstacktrace ();}}}
The created registry key:
Output result:
Subkey1value
Subkey2value
References
- Use the preferences API to store objects. This is an article from IBM that allows you to learn more about the preferences API.
- Microsoft Windows Registry description
- Description of the Microsoft Windows Registry
- In jniregistry, you can download the required package and source code.
- If you want to read jniregistry's Java Doc, you can come here.
- You can find a lot of information about the registry, registry function, registry structure, registry element size restriction, and registry value type in registry and registry reference.