給你讀寫注冊表的例子
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站建設(shè)、做網(wǎng)站與策劃設(shè)計,酒泉網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:酒泉等地區(qū)。酒泉做網(wǎng)站價格咨詢:18980820575
public class UsingReg {
public static final String REALKEY= "com.rgagnon.foo";
public static void main(String[] args){
new UsingReg().doit();
}
public void doit() {
// write into HKCU\Software\Javasoft\Prefs\com.rgagnon.foo
Preferences p = Preferences.userRoot();
p.put(REALKEY, "bar");
// read back from HKEY_CURRENT_USER
System.out.println(p);
System.out.println(p.get(REALKEY, "HKCU houston we have a problem"));
// write into HKLM\Software\Javasoft\Prefs\com.rgagnon.foo
p = Preferences.systemRoot();
p.put(REALKEY, "barbar");
// read back from HKEY_LOCAL_MACHINE
System.out.println(p);
System.out.println(p.get(REALKEY, "HKLM houston we have a problem"));
}
}
JDK1.4引入了Preferences類, 用于設(shè)置用戶的首選項,
對于Windows平臺就是操作注冊表了, 下面的程序就可以往注冊表里寫幾個值,
運行完畢后, 打開regedit, 搜尋一下, 看看注冊表里是不是有了變化了^_^
(下面注釋掉的那段代碼給恢復(fù), 就能把寫入的注冊鍵值刪除, 很方便啊)
至于讀注冊表項, 比寫還方便, 查JDK API文檔吧..
// PrefsDemo.java
import java.io.*;
import java.util.prefs.*;
public class PrefsDemo {
public static void main(String args[])
{
String keys[] =
{"sunway","copyright","author"};
String values[] =
{"sunway technology company","copyright 2002","turbochen@163.com"};
/* 建立一個位于user root下的/com/sunway/spc節(jié)點參數(shù)項*/
Preferences prefsdemo =
Preferences.userRoot().node("/com/sunway/spc");
/* 儲存參數(shù)項*/
for (int i=0 ; i keys.length; i++)
{
prefsdemo.put(keys[i], values[i]);
}
/* 導(dǎo)出到XML文件 */
try
{
FileOutputStream fos = new FileOutputStream("prefsdemo.xml");
prefsdemo.exportNode(fos);
} catch (Exception e)
{
System.err.println("Cannot export nodes: " + e);
}
/* 去掉注釋可以清除注冊表中的參數(shù)項*/
/*try
{
prefsdemo.removeNode();
} catch (BackingStoreException e)
{
}*/
}
}
1.打開注冊表編輯器regedit
2.定位至下列注冊表項:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\USBSTOR
3.新建一個名為“start”的DWORD型鍵值,并將值設(shè)為4
4.退出注冊表,不需要重啟電腦,即可。
恢復(fù)使用將值改為3.
import com.ice.jni.registry.*;
import java.text.SimpleDateFormat;
public class RegeditTool {
static SimpleDateFormat shortDateFormat =new SimpleDateFormat("yyyy-MM-dd");
//把信息存儲到注冊表HKEY_LOCAL_MACHINE下的某個節(jié)點的某一變量中,有則修改,無則創(chuàng)建
public static boolean setValue(String folder, String subKeyNode, String subKeyName, String subKeyValue) {
try{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.createSubKey(subKeyNode, "");
subKey.setValue(new RegStringValue(subKey, subKeyName, subKeyValue));
subKey.closeKey();
return true;
}catch(NoSuchKeyException e) {
e.printStackTrace();
}catch(NoSuchValueException e) {
e.printStackTrace();
}catch(RegistryException e) {
e.printStackTrace();
}
return false;
}
//刪除注冊表中某節(jié)點下的某個變量
public static boolean deleteValue(String folder, String subKeyNode,String subKeyName) {
try{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.createSubKey(subKeyNode, "");
subKey.deleteValue(subKeyName);
subKey.closeKey();
return true;
}catch (NoSuchKeyException e) {
System.out.println("NOsuchKey_delete");
}catch (NoSuchValueException e) {
System.out.println("NOsuchValue_delete");
}catch(RegistryException e) {
e.printStackTrace();
}
return false;
}
//刪除注冊表中某節(jié)點下的某節(jié)點
public static boolean deleteSubKey(String folder, String subKeyNode) {
try{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE.openSubKey(folder);
software.deleteSubKey(subKeyNode);
software.closeKey();
return true;
}
catch (NoSuchKeyException e) {
e.printStackTrace();
}catch(RegistryException e) {
e.printStackTrace();
}
return false;
}
//打開注冊表項并讀出相應(yīng)的變量名的值
public static String getValue(String folder, String subKeyNode,String subKeyName) {
String value = "";
try{
RegistryKey software = Registry.HKEY_LOCAL_MACHINE
.openSubKey(folder);
RegistryKey subKey = software.openSubKey(subKeyNode);
value = subKey.getStringValue(subKeyName);
subKey.closeKey();
}catch(NoSuchKeyException e) {
value = "NoSuchKey";
// e.printStackTrace();
}catch (NoSuchValueException e) {
value = "NoSuchValue";
// e.printStackTrace();
}catch(RegistryException e) {
e.printStackTrace();
}
retur value;
}
//測試
public static void main(String[] args) {
setValue("SOFTWARE", "Microsoft\\Windows\\CurrentVersion\\Run", "test",
"C:\\1.exe");
}
}
需要用到j(luò)ni的,也就是說需要進行外調(diào)。
寫一個C的頭文件,用這個連接java和c的代碼
然后用java調(diào)用c寫好的代碼來調(diào)用c傳過來的值
java是沒有辦法直接進行注冊表修改的