java 获取注册表键值
2015-11-17 23:32
465 查看
/**
*
* @param location path in the registry
* @param key registry key
* @return registry value or null if not found
*/
public static final String readRegistry(String location, String key)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location + "\" /v " + key);
InputStream is = process.getInputStream();
StringBuilder sw = new StringBuilder();
try
{
int c;
while ((c = is.read()) != -1)
sw.append((char)c);
}
catch (IOException e)
{
}
String output = sw.toString();
// Output has the following format:
// \n<Version information>\n\n<key> <registry type> <value>\r\n\r\n
int i = output.indexOf("REG_SZ");
if (i == -1)
{
return null;
}
sw = new StringBuilder();
i += 6; // skip REG_SZ
// skip spaces or tabs
for (;;)
{
if (i > output.length())
break;
char c = output.charAt(i);
if (c != ' ' && c != '\t')
break;
++i;
}
// take everything until end of line
for (;;)
{
if (i > output.length())
break;
char c = output.charAt(i);
if (c == '\r' || c == '\n')
break;
sw.append(c);
++i;
}
return sw.toString();
}
catch (Exception e)
{
return null;
}
*
* @param location path in the registry
* @param key registry key
* @return registry value or null if not found
*/
public static final String readRegistry(String location, String key)
{
try
{
// Run reg query, then read output with StreamReader (internal class)
Process process = Runtime.getRuntime().exec("reg query " +
'"'+ location + "\" /v " + key);
InputStream is = process.getInputStream();
StringBuilder sw = new StringBuilder();
try
{
int c;
while ((c = is.read()) != -1)
sw.append((char)c);
}
catch (IOException e)
{
}
String output = sw.toString();
// Output has the following format:
// \n<Version information>\n\n<key> <registry type> <value>\r\n\r\n
int i = output.indexOf("REG_SZ");
if (i == -1)
{
return null;
}
sw = new StringBuilder();
i += 6; // skip REG_SZ
// skip spaces or tabs
for (;;)
{
if (i > output.length())
break;
char c = output.charAt(i);
if (c != ' ' && c != '\t')
break;
++i;
}
// take everything until end of line
for (;;)
{
if (i > output.length())
break;
char c = output.charAt(i);
if (c == '\r' || c == '\n')
break;
sw.append(c);
++i;
}
return sw.toString();
}
catch (Exception e)
{
return null;
}
相关文章推荐
- Java中多态性的实现
- Java反射探索-----从类加载说起
- Struts2在Action中访问WEB资源
- Struts2在Action中访问WEB资源
- struts2中struts.xml和web.xml文件解析及工作原理
- [转][Java]使用Spring配合Junit进行单元测试的总结
- Java类加载器深入探索
- java 输入输出流详解
- Java基础部分
- Java内存模型以及Volatile、Synchronize关键字的疑问
- Java装箱和拆箱的基本概念及使用
- java实现属性转换json数据
- java设计模式--两种单例模式实现
- 接口与回调
- Java位运算符
- 服务端调用js:javax.script
- Java垃圾回收机制
- 第一次spring冲刺第6天
- Java Collection
- 对象克隆+深浅拷贝