Java学习11-注册表管理

读写注册表的方式

  • Preferences
  • jRegistry

使用JDK提供的Preferences类

  • 使用条件:如果程序不关心存储库的细节,只是找一个存放数据的地方,Preferences API很适合;
  • 局限性:不同的操作系统可能Java程序不能与系统协同;
  • 首先得到Preferences的一个对象:规定在注册表的哪个位置写入节点信息,即节点,然后再用Put(String key,String value),或者putln(),tDouble()...等等来给有关项赋值
import java.util.prefs.*; 
public class Registery { 
  String[] keys = {"version", "initial", "creator"}; 
  String[] values = {"1.3", "ini.mp3", "caokai1818@sina.com"}; 
 //把相应的值储存到变量中去 
  public void writeValue() { 
 // 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(); 
  } 
} 
  • 执行之后,将在注册表的HKEY_LOCAl_MACHINE\Software\JavaSoft\prefs\javaplayer项下写入了有关值
  • 把代码中的Preferences pre = Preferences.systemRoot().node("/javaplayer");换成Preferences pre = Preferences.userRoot().node("/javaplayer");则相应的 HKEY_LOCAL_MACHINE就成为HKEY_LOCAL_USER。

用jRegistry来操作注册表

  • windows操作系统提供了操作注册表的API,因此用JNI将Java和这些APi连接起来就获得了用Java操作注册表的能力;
  • 用JNI来封装WINDOWS注册表,方便了java开发者访问windows注册表;
  • jRegistry.jar和iRegistryKey.dll,是使用jRegistry来操作注册表所必需的文件:一个是jar包,是一个包括了java类的文件;一个是动态链接库文件,提供了访问注册表所需的本地代码(即C/C++);
使用流程
  • 在JBuilder的菜单Project->Project Properties->Required Libraries中添加jRegistryKey.jar或在环境变量classpath中添加该jar文件;
  • 将jRegistryKey.dll放在工程的当前目录下;
  • 在访问注册表类中import该语句:import ca.beq.util.win32.registry.*; 该包中有两个类:RegistryKey和RegistryValue。其中RegistryKey是注册表键的java表示,它提供了creat()和delete()方法创建和删除key,枚举子键和值,set和get键的值等;RegistryValue is the Java? representation of a registry value (defined as a name, a type, and data)。
实现代码
  • 创建一个新key
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());// 
} // if 
  • 设置注册表中键的值
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 
} // if 

  • 例子
// 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(); 
  • 涉及了创建一个key,输入值,读入值,删除键

  • com.ice.jni.registry包是通过JNI(Java native interface)实现的Windows注册表操作API,可以用来访问、修改和导出Windows注册表。
包中的类
  • HexNumberFormat 用来格式化和分析十六进制整数。
  • RegBinaryValue 表示类型为REG_BINARY的注册表值。REG_BINARY是指任意形式的二进制数。
  • RegDWordValue 表示类型为REG_DWORD的注册表值。REG_DWORD是指一个32位的整数。根据该整数的字节序不同又分为REG_DWORD_LITTLE_ENDIAN和REG_DWORD_BIG_ENDIAN。在Windows中REG_DWORD和REG_DWORD_LITTLE_ENDIAN有相同的含义。
  • RegistryValue 表示任意类型的注册表值,这是一个抽象类,不能被实例化。
  • RegMultiStringValue 表示类型为REG_MULTI_SZ的注册表值。REG_MULTI_SZ是一个null-terminated的字符串的序列。
  • RegStringValue 表示类型为REG_SZ和REG_EXPAND_SZ的注册表值。REG_SZ是指一个null-terminated的字符串,REG_EXPAND_SZ是指一个含有未展开的环境变量的null-terminated的字符串。
  • Registry 这个类定义了定级项(Key),包括HKEY_CLASSES_ROOT、HKEY_CURRENT_CONFIG、HKEY_CURRENT_USER、HKEY_DYN_DATA、HKEY_LOCAL_MACHINE、HKEY_PERFORMANCE_DATA和HKEY_USERS。还定义了错误代码,这些错误代码会包含在RegistryException中。最后是一些工具方法,如dumpHexData、exportRegistryKey、getErrorMessage、getTopLevelKey、openSubkey、parseArgumentString、parseArgumentVector、splitString和usage。
  • RegistryKey 定义了注册表的一个表项(Key)和相关的一些操作。
  • ReistryKey的方法概要


    image.png

    image.png
  • 例子
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) {
       //创建注册表项并设置相应的值
       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();
       }
                  
       //打开注册表项并读出相应的值
       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();
       }                     
     }
}
  • 按理来说结果是这个样子


    image.png

可我的还是不对

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容