java注册表操作

由于java程序是“write once, run everywhere”,用java读写注册表,那程序的跨平台性就差了。java对注册表的操作,在jdk1.4以前的版本中,那是不可能的,只能用JNI来实现;然而jdk1.4之后提供的prefs包可以操作windows注册表,不过定死了root只在SOFTWARE/JavaSoft/prefs下,估计也是出于这种两难吧,又要保证所谓平台无关,还要照顾大家对windows的依赖。下面将从两方面来介绍对注册表的操作。
一、使用JDK提供的Preferences类
首先得到Preferences的一个对象,这个对象就规定了你要在注册表的哪个位置写入信息,即节点.然后再用put(String key,String value)或者putInt(),tDouble()...等来给有关项赋值。下面是Demo程序。

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=import%20java.util.prefs.*%3B%0Apublic%20class%20Registery%20%7B%0A%20%20%20%20String%5B%5D%20keys%20%3D%20%7B%22version%22%2C%20%22initial%22%2C%20%22creator%22%7D%3B%0A%20%20%20%20String%5B%5D%20values%20%3D%20%7B%221.3%22%2C%20%22ini.mp3%22%2C%20%22caokai1818%40sina.com%22%7D%3B%0A%20%2F%2F%E6%8A%8A%E7%9B%B8%E5%BA%94%E7%9A%84%E5%80%BC%E5%82%A8%E5%AD%98%E5%88%B0%E5%8F%98%E9%87%8F%E4%B8%AD%E5%8E%BB%0A%20%20%20%20public%20void%20writeValue()%20%7B%0A%20%2F%2F%20HKEY_LOCAL_MACHINE%5CSoftware%5CJavaSoft%5Cprefs%E4%B8%8B%E5%86%99%E5%85%A5%E6%B3%A8%E5%86%8C%E8%A1%A8%E5%80%BC.%0A%20%20%20%20%20%20%20%20Preferences%20pre%20%3D%20Preferences.systemRoot().node(%22%2Fjavaplayer%22)%3B%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20keys.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20pre.put(keys%2C%20values)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%20%7B%0A%20%20%20%20%20%20%20%20Registery%20reg%20%3D%20new%20Registery()%3B%0A%20%20%20%20%20%20%20%20reg.writeValue()%3B%0A%20%20%20%20%7D%0A%7D%0A"></embed>
  1. importjava.util.prefs.*;
  2. publicclassRegistery{
  3. String[]keys={"version","initial","creator"};
  4. String[]values={"1.3","ini.mp3","[email protected]"};
  5. //把相应的值储存到变量中去
  6. publicvoidwriteValue(){
  7. //HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
  8. Preferencespre=Preferences.systemRoot().node("/javaplayer");
  9. for(inti=0;i<keys.length;i++){
  10. pre.put(keys,values);
  11. }
  12. }
  13. publicstaticvoidmain(String[]args){
  14. Registeryreg=newRegistery();
  15. reg.writeValue();
  16. }
  17. }

执行上面的代码则在注册表的HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs\javaplayer项下写入了有关值.
最后再说明几点:
1:你的节点的首字母不要大写,不然在注册表中的项前就加了一个“/”;
2:注册表中的值也可以导入到一个XML文件中,具体方法见有关文档.
3:如果把代码中的Preferences pre = Preferences.systemRoot().node("/javaplayer"); 换成Preferences pre = Preferences.userRoot().node("/javaplayer");则相应的 HKEY_LOCAL_MACHINE就成为HKEY_LOCAL_USER。
二、用jRegistry 来操作注册表
jRegistry它是用JNI来封装WINDOWS注册表API,方便了java开发者访问windows注册表。首先介绍一下jRegistryKey.jar和jRegistryKey.dll,这两个文件是使用jRegistry来操作注册表所必需的文件:一个是jar包,是一个包括了java类的文件;一个是动态链接库文件,提供了访问注册表所需的本地代码(即C/C++)。
下面详细介绍一下使用流程:
1、在JBuilder的菜单Project->Project Properties->Required Libraries中添加jRegistryKey.jar或在环境变量classpath中添加该jar文件;
2、将jRegistryKey.dll放在工程的当前目录下;
3、在访问注册表类中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).
4、创建一个新key:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%20%22Software%5C%5CBEQ%20Technologies%22)%3B%0Ar.create()%3B"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software\\BEQTechnologies");
  2. r.create();


5、创建一个子键:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%22Software%22)%3B%0Ar.createSubkey(%22BEQ%20Technologies%22)%3B"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software");
  2. r.createSubkey("BEQTechnologies");


6、删除一个已存在的键值:
try {
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");
r.delete();
} // try
catch(RegistryException re) {
re.printStackTrace();
} // catch
7、枚举子键:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%22Software%22)%3B%0Aif(r.hasSubkeys())%20%7B%0A%20%20%20Iterator%20i%20%3D%20r.subkeys()%3B%0A%20%20%20while(i.hasNext())%20%7B%0A%20%20%20%20%20%20RegistryKey%20x%20%3D%20(RegistryKey)i.next()%3B%0A%20%20%20%20%20%20System.out.println(x.toString())%3B%0A%20%20%20%7D%20%2F%2F%20while%0A%7D%20%2F%2F%20if"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software");
  2. if(r.hasSubkeys()){
  3. Iteratori=r.subkeys();
  4. while(i.hasNext()){
  5. RegistryKeyx=(RegistryKey)i.next();
  6. System.out.println(x.toString());
  7. }//while
  8. }//if


8、读注册表中键的值:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%20%22Software%5C%5CBEQ%20Technologies%22)%3B%0Aif(r.hasValue(%22myValue%22))%20%7B%0A%20%20%20RegistryValue%20v%20%3D%20r.getValue(%22myValue%22)%3B%0A%20%20%20System.out.println(v.toString())%3B%2F%2F%0A%7D%20%2F%2F%20if"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software\\BEQTechnologies");
  2. if(r.hasValue("myValue")){
  3. RegistryValuev=r.getValue("myValue");
  4. System.out.println(v.toString());//
  5. }//if


注:v.toString()仅是键myValue对应的键值,若要得到myValue键对应的值数据,则需要String str = v.getDate().toSting();
9、设置注册表中键的值:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%22Software%5C%5CBEQ%20Technologies%22)%3B%0ARegistryValue%20v%20%3D%20new%20RegistryValue(%22myVal%22%2C%20ValueType.REG_SZ%2C%20%22data%22)%3B%0Ar.setValue(v)%3B%0A"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software\\BEQTechnologies");
  2. RegistryValuev=newRegistryValue("myVal",ValueType.REG_SZ,"data");
  3. r.setValue(v);


10、枚举某键的所有值:

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=RegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_CURRENT_USER%2C%20%22Software%22)%3B%0Aif(r.hasValues())%20%7B%0A%20%20%20Iterator%20i%20%3D%20r.values()%3B%0A%20%20%20while(i.hasNext())%20%7B%0A%20%20%20%20%20%20RegistryValue%20v%20%3D%20(RegistryValue)i.next()%3B%0A%20%20%20%20%20%20System.out.println(v.toString())%3B%0A%20%20%20%7D%20%2F%2F%20while%0A%7D%20%2F%2F%20if"></embed>
  1. RegistryKeyr=newRegistryKey(RootKey.HKEY_CURRENT_USER,"Software");
  2. if(r.hasValues()){
  3. Iteratori=r.values();
  4. while(i.hasNext()){
  5. RegistryValuev=(RegistryValue)i.next();
  6. System.out.println(v.toString());
  7. }//while
  8. }//if


下面是一个demo程序,仅供参考。

Java代码<embed type="application/x-shockwave-flash" width="14" height="15" src="http://www.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%2F%2F%20create%20a%20new%20key%2C%20%22Test%22%2C%20under%20HKLM%0ARegistryKey%20r%20%3D%20new%20RegistryKey(RootKey.HKEY_LOCAL_MACHINE%2C%20%22Test%22)%3B%0Aif(!r.exists())%20%7B%0Ar.create()%3B%0A%7D%20%2F%2F%20if%20%0A%0A%2F%2F%20create%20value%20entries%0ARegistryValue%20v%20%3D%20new%20RegistryValue(%22aString%22%2C%20ValueType.REG_SZ%2C%20%22test%22)%3B%0Ar.setValue(v)%3B%0A%0Av.setName(%22aDword%22)%3B%0Av.setType(ValueType.REG_DWORD)%3B%0Av.setData(new%20Integer(0x1001001))%3B%0Ar.setValue(v)%3B%0A%0A%2F%2F%20read%20value%20entries%0AIterator%20i%20%3D%20r.values()%3B%0Awhile(i.hasNext())%20%7B%0Av%20%3D%20(RegistryValue)i.next()%3B%0ASystem.out.println(v.toString())%3B%0A%7D%20%2F%2F%20while%0A%0A%2F%2F%20delete%20registry%20key%0Ar.delete()%3B%0A"></embed>
  1. //createanewkey,"Test",underHKLM
  2. RegistryKeyr=newRegistryKey(RootKey.HKEY_LOCAL_MACHINE,"Test");
  3. if(!r.exists()){
  4. r.create();
  5. }//if
  6. //createvalueentries
  7. RegistryValuev=newRegistryValue("aString",ValueType.REG_SZ,"test");
  8. r.setValue(v);
  9. v.setName("aDword");
  10. v.setType(ValueType.REG_DWORD);
  11. v.setData(newInteger(0x1001001));
  12. r.setValue(v);
  13. //readvalueentries
  14. Iteratori=r.values();
  15. while(i.hasNext()){
  16. v=(RegistryValue)i.next();
  17. System.out.println(v.toString());
  18. }//while
  19. //deleteregistrykey
  20. r.delete();

三、参照http://wangunix.iteye.com/blog/197983

你可能感兴趣的:(java,windows,jni,Flash,Go)