1. 程式人生 > >java 操作登錄檔

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程式碼 複製程式碼
  1. import java.util.prefs.*;   
  2. publicclass Registery {   
  3.     String[] keys = {"version""initial""creator"};   
  4.     String[] values = {"1.3"
    "ini.mp3""[email protected]"};   
  5. //把相應的值儲存到變數中去
  6. publicvoid writeValue() {   
  7. // HKEY_LOCAL_MACHINE/Software/JavaSoft/prefs下寫入登錄檔值.
  8.         Preferences pre = Preferences.systemRoot().node("/javaplayer");   
  9. for (int i = 0; i < keys.length; i++) {   
  10.             pre.put(keys, values);   
  11.         }   
  12.     }   
  13. publicstaticvoid main(String[] args) {   
  14.         Registery reg = new Registery();   
  15.         reg.writeValue();   
  16.     }   
  17. }  
import java.util.prefs.*;
public class Registery {
    String[] keys = {"version", "initial", "creator"};
    String[] values = {"1.3", "ini.mp3", "[email protected]
"}; //把相應的值儲存到變數中去 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: 

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software//BEQ Technologies");   
  2. r.create();  
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software//BEQ Technologies");
r.create();


5、建立一個子鍵:

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software");   
  2. r.createSubkey("BEQ Technologies");  
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、列舉子鍵:

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software");   
  2. if(r.hasSubkeys()) {   
  3.    Iterator i = r.subkeys();   
  4. while(i.hasNext()) {   
  5.       RegistryKey x = (RegistryKey)i.next();   
  6.       System.out.println(x.toString());   
  7.    } // while
  8. // if
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、讀登錄檔中鍵的值:

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software//BEQ Technologies");   
  2. if(r.hasValue("myValue")) {   
  3.    RegistryValue v = r.getValue("myValue");   
  4.    System.out.println(v.toString());//
  5. // 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


注:v.toString()僅是鍵myValue對應的鍵值,若要得到myValue鍵對應的值資料,則需要String str = v.getDate().toSting();
9、設定登錄檔中鍵的值:

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software//BEQ Technologies");   
  2. RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data");   
  3. r.setValue(v);  
RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software//BEQ Technologies");
RegistryValue v = new RegistryValue("myVal", ValueType.REG_SZ, "data");
r.setValue(v);


10、列舉某鍵的所有值:

Java程式碼 複製程式碼
  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software");   
  2. if(r.hasValues()) {   
  3.    Iterator i = r.values();   
  4. while(i.hasNext()) {   
  5.       RegistryValue v = (RegistryValue)i.next();   
  6.       System.out.println(v.toString());   
  7.    } // while
  8. // if
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程式,僅供參考。
 

Java程式碼 複製程式碼
  1. // create a new key, "Test", under HKLM
  2. RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "Test");   
  3. if(!r.exists()) {   
  4. r.create();   
  5. // if 
  6. // create value entries
  7. RegistryValue v = new RegistryValue("aString", ValueType.REG_SZ, "test");   
  8. r.setValue(v);   
  9. v.setName("aDword");   
  10. v.setType(ValueType.REG_DWORD);   
  11. v.setData(new Integer(0x1001001));   
  12. r.setValue(v);   
  13. // read value entries
  14. Iterator i = r.values();   
  15. while(i.hasNext()) {   
  16. v = (RegistryValue)i.next();   
  17. System.out.println(v.toString());   
  18. // while
  19. // delete registry key
  20. r.delete();