一种是使用java提供的,但是只能获取HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Prefs目录下的,另外一种cmd命令方式,可以指定任意路径。
代码如下:
package com.reg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
public class Registery {
String[] keys = { "version", "initial", "creator" };
String[] values = { "1.3", "ini.mp3", "caokai1818@sina.com" };
// 把相应的值储存到变量中去
public void writeValue() {
// HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.
Preferences pre = Preferences.systemRoot().node("/javaplayer");
for (int i = 0; i < keys.length; i++) {
pre.put(keys[i], values[i]);
}
}
/***
* 根据key获取value
*
*/
public String getValue(String key) {
Preferences pre = Preferences.systemRoot().node("/javaplayer");
return pre.get(key, "time");
}
/***
* 清除注册表
*
* @throws BackingStoreException
*/
public void clearValue() throws BackingStoreException {
Preferences pre = Preferences.systemRoot().node("/javaplayer");
pre.clear();
}
public static void main(String[] args) {
Registery reg = new Registery();
reg.writeValue();
System.out.println(reg.getValue("version"));
//可以读取任意路径下的、
try {
Process ps = null;
ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\javaplayer");
ps.getOutputStream().close();
InputStreamReader i = new InputStreamReader(ps.getInputStream());
String line;
BufferedReader ir = new BufferedReader(i);
while ((line = ir.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
网友评论