注册表java参数_Java通过CMD方式读取注册表任意键值对代码实践

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

28530401acdbadbcc06fa8cb3a3e83eb.png

直接上代码:

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

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

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值