Preferences and jRegistry are the ways to read and write the registry

  • 2021-01-18 06:28:50
  • OfStack

This paper mainly studies two ways of Java reading and writing registry, Preferences and jRegistry, which are introduced as follows.

Because the java program is "write once, run everywhere", using java to read and write the registry, that program cross-platform is poor. java operations on the registry, which were not possible in previous versions of jdk1.4, can only be implemented in JNI; However, prefs packages provided after jdk1.4 can operate the windows registry, but it is determined that root only operates under SOFTWARE/JavaSoft/prefs. It is also due to this dilemma, but also to ensure that the so-called platform independent, but also to take care of the dependence on windows. The following are two aspects of working with the registry.

1. Use the Preferences class provided by JDK

You first get an object in Preferences that specifies where you want to write information in the registry, the node. Then use put(String key,String value) or putInt(),tDouble()... And so on to assign a value to the term in question. The following is the Demo program.


import java.util.prefs.*; 
public class Registery { 
  String[] keys = {"version", "initial", "creator"}; 
  String[] values = {"1.3", "ini.mp3", "caokai1818@sina.com"}; 
 // Store the corresponding value in a variable  
  public void writeValue() { 
 // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs Write the registry values below . 
    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(); 
  } 
} 

Executing the above code writes the relevant values in the registry under the entry HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\javaplayer.

Finally, a few more points:

Do not capitalize the first letter of your node, otherwise you will add a "/" before the entry in the registry; Values from the registry can also be imported into an XML file, as shown in the documentation. pre = Preferences.systemRoot ().node("/javaplayer"); pre = Preferences.systemRoot (). Preferences pre = Preferences.userRoot ().node("/javaplayer"); Preferences = Preferences.userRoot (). The corresponding HKEY_LOCAL_MACHINE becomes HKEY_LOCAL_USER.

2. Use ES73en to manipulate the registry

jRegistry is used to encapsulate the WINDOWS registry API, which is convenient for java developers to access the windows registry. jRegistryKey.jar and jRegistryKey.dll are two files that are required to operate the registry using jRegistry. One is the jar package, which is a file containing the java class. 1 is a dynamic link library file that provides the native code needed to access the registry (i.e. C/C++).

The following procedures are described in detail:

In the JBuilder menu Project- > Project Properties- > jRegistryKey to Required Libraries.jar or to classpath to environment variable classpath; Put jRegistryKey.dll in the project's current directory; In the access to the registry class import this statement: 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, which provides creat() and delete() methods to create and delete key, enumerate subkeys and values, set and get key values, etc. RegistryValue is the Java? representation of a registry value (defined as a name, a type, and data).

The implementation code

Create a new key:

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, 
 "Software\\BEQ Technologies"); 
r.create(); 
Create 1 child key:

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software"); 
r.createSubkey("BEQ Technologies"); 
Delete 1 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 of 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 value of the key 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 just the key myValue corresponding key value, if you want to get myValue key corresponding to the value of the data, you need to String str = v. getDate () toSting ();

Set key values 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 for 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 an 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(); 

conclusion

Above is this article about Java read and write registry way Preferences and jRegistry all content, I hope to help you. Interested friends can continue to refer to the site of other related topics, if there are shortcomings, welcome to leave a message to point out. Thank you for your support!


Related articles: