熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java對Windows注冊表進行增刪查

2022-06-13   來源: Java核心技術 

  代碼描述

  用java對windows注冊表進行編輯是一個比較困難的任務因為java的平台無關性但是java提供了Runtime類它允許把原始的command命令直接發生送給OS所以可以通過這個類來實現對注冊表簡單有效的管理

  這裡允許的基本功能是添加刪除和查詢注冊表信息根據key查詢出value和type分別存放在不同的變量中

  //主要的方法

  add(String location String key String type String value);

  delete(String location String key);

  query(String location String key);

  //查詢消息用到的方法

  getKey() => 檢索的key名

  getValue() => 根據key得到的value

  getType() => 根據key得到的數據類型 (REG_SZ REG_BINARY 等)

  調用方法

  例:

  public static void main(String[] args) {

  try {

  RegKeyManager rkm = new RegKeyManager();

  //            位系統的注冊表目錄

  //            rkmquery(HKEY_LOCAL_MACHINE\\SOFTWARE\\WowNode\\Tencent\\QQ Install);

  //            位系統的注冊表目錄

  rkmquery(HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ Install);

  Systemoutprintln(KEY: + rkmgetKey() + DATA TYPE: + rkmgetType() + DATA VALUE: + rkmgetValue());

  //            rkmadd(HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQTESTINGREG_SZVALUE DATA);

  //            rkmdelete(HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQTESTING);

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  注:位系統注冊表路徑是不同的上面的代碼實現了在qq的注冊表路徑下查詢出qq的安裝路徑(對於獲取QQ的安裝路徑前段時間寫過一篇類似的文章如果你感興趣的話可以參看JAVA通過注冊表獲取軟件的安裝路徑)在安裝路徑下添加一個注冊表信息然後刪除添加的信息

  package languageexpressjava;

  import javaio*;

  public class RegKeyManager {

  private final String TYPES[] = { SZ BINARY DWORD QWORD DWORD_LITTLE_ENDIAN QWORD_LITTLE_ENDIAN DWORD_BIG_ENDIAN EXPAND_SZ LINK MULTI_SZ NONE RESOURCE_LIST };

  private String type = value = key = ;

  protected void query(String loc String k) throws Exception {

  Process p = RuntimegetRuntime()exec(reg QUERY \ + loc + \ /v \ + k + \);

  BufferedReader in = new BufferedReader( new InputStreamReader( pgetInputStream() ) );

  String out = ;

  while ( ( out = inreadLine() ) != null ) {

  if (outmatches((*)\\s+REG_(*))) { break; }

  }

  inclose();

  String str[] = outsplit(    );

  int b = ;

  for (int a=; a < strlength; a++) {

  if ( str[a]replace( )matches(\\S+) ) {

  switch (b) {

  case : key = str[a]; break;

  case : type = str[a]; break;

  case : value = str[a]; break;

  }

  b++;

  }

  }

  }

  protected String getKey() { return key; }

  protected String getType() { return type; }

  protected String getValue() { return value; }

  protected boolean add(String loc String name String dType String value) throws Exception {

  boolean comp = false valid = false;

  for (int a = ; a < TYPESlength; a++) {

  if (dTypeequalsIgnoreCase(REG_ + TYPES[a])) { valid = true; break; }

  }

  if ( valid ) {

  Process p = RuntimegetRuntime()exec(reg ADD \ + loc + \ /v \ + name + \ /t \ + dType + \ /d \ + value + \);

  BufferedReader in = new BufferedReader( new InputStreamReader( pgetInputStream() ) );

  String out = ;

  while ( (out = inreadLine() ) != null ) {

  if (outequalsIgnoreCase(The operation completed successfully)) { comp = true; }

  }

  inclose();

  }

  return comp;

  }

  protected boolean delete(String loc String key) throws Exception {

  boolean comp = false;

  Process p = RuntimegetRuntime()exec(reg DELETE \ + loc + \ /v \ + key + \ /f);

  BufferedReader in = new BufferedReader( new InputStreamReader( pgetInputStream() ) );

  String out = ;

  while ( ( out = inreadLine() ) != null ) {

  if (outequalsIgnoreCase(The operation completed successfully)) { comp = true; }

  }

  inclose();

  return comp;

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26615.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.