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

作者:yaominghui 时间:2021-10-10 19:56:40 

需要读取如图所示注册表【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】的值。

来源:https://www.jianshu.com/p/f7a62b9aaa13

标签:hava,cmd,注册表,键,值
0
投稿

猜你喜欢

  • jdk线程池的实现

    2023-07-05 18:44:35
  • java 创建线程的几种方式

    2023-10-29 19:40:16
  • 5种Android数据存储方式汇总

    2023-08-06 06:49:04
  • java身份证验证代码实现

    2023-12-09 16:10:50
  • 解决运行jar包出错:ClassNotFoundException问题

    2021-09-09 04:58:41
  • Java利用cors实现跨域请求实例

    2023-02-24 14:57:35
  • Mybatis分解式查询使用方法

    2023-08-16 04:15:06
  • Android自定义dialog简单实现方法

    2021-07-29 17:10:00
  • Spring Batch轻量级批处理框架实战

    2023-01-08 00:24:23
  • SpringBoot升级指定jackson版本的问题

    2022-05-31 13:18:49
  • java堆排序概念原理介绍

    2021-08-30 12:31:58
  • SpringMvc+Mybatis+Pagehelper分页详解

    2021-08-13 14:15:11
  • 简单捋捋@RequestParam 和 @RequestBody的使用

    2022-11-02 16:19:20
  • 解决idea 项目编译后没有class文件的问题

    2023-11-04 07:55:06
  • Java中定时器Timer致命缺点案例详解

    2022-08-27 08:38:45
  • java底层JDK Logging日志模块处理细节深入分析

    2023-02-04 12:47:31
  • 阿里面试Nacos配置中心交互模型是push还是pull原理解析

    2023-04-09 06:40:54
  • 使用mutex实现应用程序单实例运行代码分享

    2023-09-18 22:45:11
  • 浅谈Servlet开发技术基础

    2023-01-17 04:03:15
  • springmvc后台基于@ModelAttribute获取表单提交的数据

    2023-08-05 12:29:35
  • asp之家 软件编程 m.aspxhome.com