欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-04 16:56:47
需要读取如图所示注册表【hkey_local_machine\software\easydrv7】节点下的【datetime】的值 直接上代码: pac...

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

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

直接上代码:

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】的值。

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