当前位置: 技术问答>java相关
java能对注册表进行操作吗?
来源: 互联网 发布时间:2015-09-30
本文导语: 该如何实现? | 通过JNI实现,有5个文件: 1、Win32RegKey.java /** * @version 1.00 1997-07-01 * @author Cay Horstmann */ import java.util.*; public class Win32RegKey { public Win32RegKey(int theRoot, String thePath...
该如何实现?
|
通过JNI实现,有5个文件:
1、Win32RegKey.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/
import java.util.*;
public class Win32RegKey
{ public Win32RegKey(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}
public Enumeration names()
{ return new Win32RegKeyNameEnumeration(root, path);
}
public native Object getValue(String name);
public native void setValue(String name, Object value);
public static final int HKEY_CLASSES_ROOT = 0x80000000;
public static final int HKEY_CURRENT_USER = 0x80000001;
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
public static final int HKEY_USERS = 0x80000003;
public static final int HKEY_CURRENT_CONFIG = 0x80000005;
public static final int HKEY_DYN_DATA = 0x80000006;
private int root;
private String path;
static
{ System.loadLibrary("Win32RegKey");
}
}
class Win32RegKeyNameEnumeration implements Enumeration
{ Win32RegKeyNameEnumeration(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}
public native Object nextElement();
public native boolean hasMoreElements();
private int root;
private String path;
private int index = -1;
private int hkey = 0;
private int maxsize;
private int count;
}
class Win32RegKeyException extends RuntimeException
{ public Win32RegKeyException() {}
public Win32RegKeyException(String why)
{ super(why);
}
}
2、Win32RegKeyTest.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/
import java.util.*;
public class Win32RegKeyTest
{ public static void main(String[] args)
{ Win32RegKey key = new Win32RegKey(
Win32RegKey.HKEY_CURRENT_USER,
"Software\Microsoft\MS Setup (ACME)\User Info");
key.setValue("Default user", "Bozo the clown");
key.setValue("Lucky number", new Integer(13));
key.setValue("Small primes", new byte[]
{ 2, 3, 5, 7, 11 });
Enumeration enum = key.names();
while (enum.hasMoreElements())
{ String name = (String)enum.nextElement();
System.out.print(name + " = ");
Object value = key.getValue(name);
if (value instanceof byte[])
{ byte[] bvalue = (byte[])value;
for (int i = 0; i
1、Win32RegKey.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/
import java.util.*;
public class Win32RegKey
{ public Win32RegKey(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}
public Enumeration names()
{ return new Win32RegKeyNameEnumeration(root, path);
}
public native Object getValue(String name);
public native void setValue(String name, Object value);
public static final int HKEY_CLASSES_ROOT = 0x80000000;
public static final int HKEY_CURRENT_USER = 0x80000001;
public static final int HKEY_LOCAL_MACHINE = 0x80000002;
public static final int HKEY_USERS = 0x80000003;
public static final int HKEY_CURRENT_CONFIG = 0x80000005;
public static final int HKEY_DYN_DATA = 0x80000006;
private int root;
private String path;
static
{ System.loadLibrary("Win32RegKey");
}
}
class Win32RegKeyNameEnumeration implements Enumeration
{ Win32RegKeyNameEnumeration(int theRoot, String thePath)
{ root = theRoot;
path = thePath;
}
public native Object nextElement();
public native boolean hasMoreElements();
private int root;
private String path;
private int index = -1;
private int hkey = 0;
private int maxsize;
private int count;
}
class Win32RegKeyException extends RuntimeException
{ public Win32RegKeyException() {}
public Win32RegKeyException(String why)
{ super(why);
}
}
2、Win32RegKeyTest.java
/**
* @version 1.00 1997-07-01
* @author Cay Horstmann
*/
import java.util.*;
public class Win32RegKeyTest
{ public static void main(String[] args)
{ Win32RegKey key = new Win32RegKey(
Win32RegKey.HKEY_CURRENT_USER,
"Software\Microsoft\MS Setup (ACME)\User Info");
key.setValue("Default user", "Bozo the clown");
key.setValue("Lucky number", new Integer(13));
key.setValue("Small primes", new byte[]
{ 2, 3, 5, 7, 11 });
Enumeration enum = key.names();
while (enum.hasMoreElements())
{ String name = (String)enum.nextElement();
System.out.print(name + " = ");
Object value = key.getValue(name);
if (value instanceof byte[])
{ byte[] bvalue = (byte[])value;
for (int i = 0; i