我们都知道Java语言是和操作系统无关的,所以一般某一操作系统所独有的功能,Java实现起来起比较困难。而注册表好是Windows系统所独有的,Java能不能操作呢,事实证明Java是可以操作Windows的注册表的,只是操作范围比较有限。
Java只能对[HKEY_CURRENT_USER\Software\JavaSoft\Prefs],和[HKEY_LOCAL_MACHINE \Software\JavaSoft\Prefs]下的内容进行操作。
import java.io.*;
import java.util.prefs.*;
public class Register {
public Register() {
String denominations[] = {"One", "Two", "Five", "Ten", "Twenty"};
String pictures[] = {"Washington", "Jefferson", "Lincoln", "Hamilton", "Jackson"};
Preferences prefs = Preferences.userRoot().node("/net/zukowski/ibm");
for (int i = 0, n = denominations.length; i < class="blsp-spelling-error" id="SPELLING_ERROR_9">prefs.put(denominations[i], pictures[i]);
}
try {
String keys[] = prefs.keys();
for (int i = 0, n = keys.length; i < class="blsp-spelling-error" id="SPELLING_ERROR_11">prefs.get(keys[i], "Unknown"));
}
} catch (BackingStoreException e) {
System.err.println("Unable to read backing store: " + e);
}
Preferences child = Preferences.userRoot().node("/net/zukowski/ibm/foo");
try {
FileOutputStream fos = new FileOutputStream("prefs.out");
prefs.exportNode(fos);
}
catch (Exception e) {
System.err.println("Unable to export nodes: " + e);
}
try {
prefs.removeNode();
} catch (BackingStoreException e) {
System.err.println("Unable to access backing store: " + e);
}
}
public static void main(String[] args) {
Register main1 = new Register();
}
}
不知道这段代码在Linux下运行会有什么效果。
没有评论:
发表评论