用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java对windows注册表进行增删查

2015-02-07 作者: java源代码大全举报

[java]代码库

package info.itlanguageexpress.java;
import java.io.*;
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 = Runtime.getRuntime().exec("reg QUERY \"" + loc + "\" /v \"" + k + "\"");
        BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
        String out = "";
        while ( ( out = in.readLine() ) != null ) {
            if (out.matches("(.*)\\s+REG_(.*)")) { break; }
        }
        in.close();
        String str[] = out.split("    ");
        int b = 0;
        for (int a=0; a < str.length; a++) {
            if ( str[a].replace(" ", "").matches("\\S+") ) {
                switch (b) {
                    case 0: key = str[a]; break;
                    case 1: type = str[a]; break;
                    case 2: 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 = 0; a < TYPES.length; a++) {
            if (dType.equalsIgnoreCase("REG_" + TYPES[a])) { valid = true; break; }
        }
        if ( valid ) {
            Process p = Runtime.getRuntime().exec("reg ADD \"" + loc + "\" /v \"" + name + "\" /t \"" + dType + "\" /d \"" + value + "\"");
            BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
            String out = "";
            while ( (out = in.readLine() ) != null ) {
                if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }
            }
            in.close();
        }
        return comp;
    }
    protected boolean delete(String loc, String key) throws Exception {
        boolean comp = false;
        Process p = Runtime.getRuntime().exec("reg DELETE \"" + loc + "\" /v \"" + key + "\" /f");
        BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
        String out = "";
        while ( ( out = in.readLine() ) != null ) {
            if (out.equalsIgnoreCase("The operation completed successfully.")) { comp = true; }
        }
        in.close();
        return comp;
    }
}
//源代码片段来自云代码http://yuncode.net
			

    public static void main(String[] args) {
        try {
            RegKeyManager rkm = new RegKeyManager();
//            64位系统的注册表目录
//            rkm.query("HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Tencent\\QQ2009", "Install");
//            32位系统的注册表目录
            rkm.query("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009", "Install");
            System.out.println("KEY: " + rkm.getKey() + " DATA TYPE: " + rkm.getType() + " DATA VALUE: " + rkm.getValue());
//            rkm.add("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009","TESTING","REG_SZ","VALUE DATA");
//            rkm.delete("HKEY_LOCAL_MACHINE\\SOFTWARE\\Tencent\\QQ2009","TESTING");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...