木木甫 2017-02-25 06:43 采纳率: 35.7%
浏览 963
已采纳

java创建注册表失败,????

 /**
 * Created by demo on 2017/2/25.
 */
import com.ice.jni.registry.NoSuchKeyException;
import com.ice.jni.registry.RegStringValue;
import com.ice.jni.registry.Registry;
import com.ice.jni.registry.RegistryException;
import com.ice.jni.registry.RegistryKey;
/**
 * @author solo L
 *
 */
class JNIRegistryTest {

    public  void openRegistry(){
        //打开注册表项并读出相应的值
        try {
            RegistryKey software = Registry.HKEY_LOCAL_MACHINE.
                    openSubKey("SOFTWARE");
            RegistryKey subKey = software.openSubKey("ymbizhi");
            String subKey1Value = subKey.getStringValue("Channel");
            String subKey2Value = subKey.getStringValue("InstallTime");
            System.out.println(subKey1Value);
            System.out.println(subKey2Value);
            subKey.closeKey();
        } catch (NoSuchKeyException e) {
            e.printStackTrace();
        } catch (RegistryException e) {
            e.printStackTrace();
        }
    }
    public void createRegist(){
        //创建注册表项并设置相应的值
        try {
            RegistryKey software = Registry.HKEY_LOCAL_MACHINE
                    .openSubKey("SOFTWARE");
            RegistryKey subKey = software.createSubKey("SubKeyName", "");
            subKey.setValue(new RegStringValue(subKey, "subKey1",
                    "subKey1Value"));
            subKey.setValue(new RegStringValue(subKey, "subKey2",
                    "subKey2Value"));
            subKey.closeKey();
        } catch (NoSuchKeyException e) {
            e.printStackTrace();
        } catch (RegistryException e) {
            e.printStackTrace();
        }
    }

}

单元测试:

 import org.junit.Test;

import static org.junit.Assert.*;

/**
 * Created by demo on 2017/2/25.
 */
public class JNIRegistryTestTest {

    private JNIRegistryTest regist = new JNIRegistryTest();

    @Test
    public void openRegistry() throws Exception {
        regist.openRegistry();
    }

    @Test
    public void createRegist() throws Exception {
        regist.createRegist();
    }

}

图片说明

  • 写回答

3条回答 默认 最新

  • 木木甫 2017-02-25 08:45
    关注

    最后我换了种方法,用的是java.util.Prefs解决的注册表创建问题,com.ice只能读,而不能创建。因为权限

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?