Java通過CMD方式讀取注冊表任意鍵值對代碼實踐
需要讀取如圖所示注冊表【HKEY_LOCAL_MACHINE\SOFTWARE\EasyDrv7】節(jié)點下的【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>
* 讀取注冊表指定節(jié)點所有的鍵值對
* </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>
* 讀取注冊表指定節(jié)點指定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】 讀取節(jié)點全部鍵值對,再通過解析得到我們所需要的【DateTime】的值。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
聊聊Kotlin?中?lateinit?和?lazy?的原理區(qū)別
使用 Kotlin 進行開發(fā),對于 latelinit 和 lazy 肯定不陌生。但其原理上的區(qū)別,可能鮮少了解過,借著本篇文章普及下這方面的知識,感興趣的朋友一起看看吧2022-07-07
詳解poi+springmvc+springjdbc導入導出excel實例
本篇文章主要介紹了poi+springmvc+springjdbc導入導出excel實例,非常具有實用價值,需要的朋友可以參考下。2017-01-01
mybatis執(zhí)行批量更新batch update 的方法(oracle,mysql兩種)
這篇文章主要介紹了mybatis執(zhí)行批量更新batch update 的方法,提供oracle和mysql兩種方法,非常不錯,需要的朋友參考下2017-01-01
eclipse maven maven-archetype-webapp 創(chuàng)建失敗問題解決
這篇文章主要介紹了eclipse maven maven-archetype-webapp 創(chuàng)建失敗問題解決的相關資料,需要的朋友可以參考下2016-12-12

