打開注冊表方式:電腦上的windows鍵加r鍵打開運行程序->輸入指令regedit->隨即系統便會提示你是否要運行,選擇“是“就行了
由於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程序。
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項下寫入了有關值.
最后再說明幾點:
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:
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");
- r.create();
5、創建一個子鍵:
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software");
- r.createSubkey("BEQ Technologies");
6、刪除一個已存在的鍵值:
- try {
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");
- r.delete();
- } // try
- catch(RegistryException re) {
- re.printStackTrace();
- } // catch
7、枚舉子鍵:
- 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
8、讀注冊表中鍵的值:
- 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
注:v.toString()僅是鍵myValue對應的鍵值,若要得到myValue鍵對應的值數據,則需要String str = v.getDate().toSting();
9、設置注冊表中鍵的值:
- RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");
- RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data");
- r.setValue(v);
10、枚舉某鍵的所有值:
- 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
下面是一個demo程序,僅供參考。
// 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();