当前位置: 移动技术网 > IT编程>开发语言>Java > Java通过CMD方式读取注册表任意键值对代码实践

Java通过CMD方式读取注册表任意键值对代码实践

2019年07月19日  | 移动技术网IT编程  | 我要评论

再生传奇txt下载,nhdta-141,aeninu

需要读取如图所示注册表【hkey_local_machine\software\easydrv7】节点下的【datetime】的值

直接上代码:

package com.beibei.common.util.cmd;
import java.io.bufferedreader;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.util.hashmap;
import java.util.map;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
/**
* 注册表操作工具类
* @author 北北
* @date 2019年6月19日下午8:21:02
*/
public class registryutil {
private static logger logger = loggerfactory.getlogger(registryutil.class);
/**
* <pre>
* 读取注册表指定节点所有的键值对
* </pre>
* @author 北北
* @date 2019年6月19日下午8:43:56
* @param nodepath
* @return
*/
public static map<string, string> readnode(string nodepath) {
map<string, string> regmap = new hashmap<>();
try {
process process = runtime.getruntime().exec("reg query " + nodepath);
process.getoutputstream().close();
inputstreamreader isr = new inputstreamreader(process.getinputstream());
string line = null;
bufferedreader ir = new bufferedreader(isr);
while ((line = ir.readline()) != null) {
string[] arr = line.split(" ");
if(arr.length != 4){
continue;
}
regmap.put(arr[1], arr[3]);
}
process.destroy();
} catch (ioexception e) {
logger.error("读取注册表失败, nodepath: " + nodepath, e);
}
return regmap;
}
/**
* <pre>
* 读取注册表指定节点指定key的值
* </pre>
* @author 北北
* @date 2019年6月19日下午8:43:24
* @param nodepath
* @param key
* @return
*/
public static string readvalue(string nodepath, string key) {
map<string, string> regmap = readnode(nodepath);
return regmap.get(key);
}
public static void main(string[] args) {
string paramvalue = registryutil.readvalue("hkey_local_machine\\software\\easydrv7", "datetime");
system.out.println(paramvalue);
}
}

其原理是通过cmd命令【reg query hkey_local_machine\software\easydrv7】 读取节点全部键值对,再通过解析得到我们所需要的【datetime】的值。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网