[Java] java对windows注册表进行增删查 →→→→→进入此内容的聊天室

来自 , 2019-10-02, 写在 Java, 查看 177 次.
URL http://www.code666.cn/view/ed1d136f
  1. package info.itlanguageexpress.java;
  2. import java.io.*;
  3. public class RegKeyManager {
  4.     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" };
  5.     private String type = "", value = "", key = "";
  6.     protected void query(String loc, String k) throws Exception {
  7.         Process p = Runtime.getRuntime().exec("reg QUERY \"" + loc + "\" /v \"" + k + "\"");
  8.         BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
  9.         String out = "";
  10.         while ( ( out = in.readLine() ) != null ) {
  11.             if (out.matches("(.*)\\s+REG_(.*)")) { break; }
  12.         }
  13.         in.close();
  14.         String str[] = out.split("    ");
  15.         int b = 0;
  16.         for (int a=0; a < str.length; a++) {
  17.             if ( str[a].replace(" ", "").matches("\\S+") ) {
  18.                 switch (b) {
  19.                     case 0: key = str[a]; break;
  20.                     case 1: type = str[a]; break;
  21.                     case 2: value = str[a]; break;
  22.                 }
  23.                 b++;
  24.             }
  25.         }
  26.     }
  27.     protected String getKey() { return key; }
  28.     protected String getType() { return type; }
  29.     protected String getValue() { return value; }
  30.     protected boolean add(String loc, String name, String dType, String value) throws Exception {
  31.         boolean comp = false, valid = false;
  32.         for (int a = 0; a < TYPES.length; a++) {
  33.             if (dType.equalsIgnoreCase("REG_" + TYPES[a])) { valid = true; break; }
  34.         }
  35.         if ( valid ) {
  36.             Process p = Runtime.getRuntime().exec("reg ADD \"" + loc + "\" /v \"" + name + "\" /t \"" + dType + "\" /d \"" + value + "\"");
  37.             BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
  38.             String out = "";
  39.             while ( (out = in.readLine() ) != null ) {
  40.                 if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }
  41.             }
  42.             in.close();
  43.         }
  44.         return comp;
  45.     }
  46.     protected boolean delete(String loc, String key) throws Exception {
  47.         boolean comp = false;
  48.         Process p = Runtime.getRuntime().exec("reg DELETE \"" + loc + "\" /v \"" + key + "\" /f");
  49.         BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
  50.         String out = "";
  51.         while ( ( out = in.readLine() ) != null ) {
  52.             if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }
  53.         }
  54.         in.close();
  55.         return comp;
  56.     }
  57. }
  58.  
  59.  
  60. //java/5762

回复 "java对windows注册表进行增删查"

这儿你可以回复上面这条便签

captcha