java下操做註冊表方法

因爲java程序是「write once, run everywhere」,用java讀寫註冊表,那程序的跨平臺性就差了。java對註冊表的操做,在jdk1.4之前的版本中,那是不可能的,只能用JNI來實現;然而jdk1.4以後提供的prefs包能夠操做windows註冊表,不過定死了root只在SOFTWARE/JavaSoft/prefs下,估計也是出於這種兩難吧,又要保證所謂平臺無關,還要照顧你們對windows的依賴。下面將從兩方面來介紹對註冊表的操做。
1、 使用JDK提供的Preferences類
 首先獲得Preferences的一個對象,這個對象就規定了你要在註冊表的哪一個位置寫入信息,即節點.而後再用put(String key,String value)或者putInt(),tDouble()...等來給有關項賦值。下面是Demo程序。java

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。
2、 用jRegistry 來操做註冊表
 jRegistry它是用JNI來封裝WINDOWS註冊表API,方便了java開發者訪問windows註冊表。首先介紹一下jRegistryKey.jar和jRegistryKey.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:  node

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\BEQ Technologies");
r.create();


五、建立一個子鍵:windows

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
七、枚舉子鍵:ui

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


八、讀註冊表中鍵的值:spa

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();
九、設置註冊表中鍵的值:unix

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\BEQ Technologies");
RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data");
r.setValue(v);


十、枚舉某鍵的全部值:code

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();

3、參照http://wangunix.iteye.com/blog/197983blog