需要读取如图所示注册表【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);
/**
*
* 读取注册表指定节点所有的键值对
*
* @author 北北
* @date 2019年6月19日下午8:43:56
* @param nodepath
* @return
*/
public static map readnode(string nodepath) {
map 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;
}
/**
*
* 读取注册表指定节点指定key的值
*
* @author 北北
* @date 2019年6月19日下午8:43:24
* @param nodepath
* @param key
* @return
*/
public static string readvalue(string nodepath, string key) {
map 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】的值。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。
希望与广大网友互动??
点此进行留言吧!