锁定老帖子 主题:java下操作注册表方法
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-05-23
由于java程序是“write once, run everywhere”,用java读写注册表,那程序的跨平台性就差了。java对注册表的操作,在jdk1.4以前的版本中,那是不可能的,只能用JNI来实现;然而jdk1.4之后提供的prefs包可以操作windows注册表,不过定死了root只在SOFTWARE/JavaSoft/prefs下,估计也是出于这种两难吧,又要保证所谓平台无关,还要照顾大家对windows的依赖。下面将从两方面来介绍对注册表的操作。 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项下写入了有关值. 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");
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(); 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
浏览 14969 次