通过java往注册表里写入信息

通过java往注册表里写入信息

package com.ardo.api.reg;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.prefs.Preferences;

public class Registery {
	
	public static void writeValid(String valid){
		Preferences pre = Preferences.systemRoot().node("/"+doc); 
		pre.put("valid", "["+valid+"]");
	}
	
	public static void writeStime(String stime){
		Preferences pre = Preferences.systemRoot().node("/"+doc); 
		pre.put("stime", "("+stime+")");
	}
	
	public static String readValid(){
    	String content = "";
    	String valid = "";
    	String stime = "";
        try {    
            Process ps = null;    
            ps = Runtime.getRuntime().exec("reg query " + path + doc);
            ps.getOutputStream().close();    
            InputStreamReader i = new InputStreamReader(ps.getInputStream());    
            String line;    
            BufferedReader ir = new BufferedReader(i);    
            while ((line = ir.readLine()) != null) {    
                content += line;
            }    
            valid = content.substring(content.indexOf("[")+1, content.indexOf("]"));
            stime = content.substring(content.indexOf("(")+1, content.indexOf(")"));
        } catch (IOException e) {    
            e.printStackTrace();     
        } 
        return valid+"@"+stime;
    }
	
	public static void main(String[] args) {  
		//String content = readValid();
		//System.out.println("-------"+content);
		
		writeStime("20170126");
    }  
	
	private static String path = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\";
    private static String doc = "register";
}

package com.ardo.utils;

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", "valid", "creator" };  
    String[] values = { "1.3", "[20170325]", "duzhiwei2011@sina.com" };  
    
    
  
    // 把相应的值储存到变量中去  
    public void writeValue() {  
        // HKEY_LOCAL_MACHINE\Software\JavaSoft\prefs下写入注册表值.  
        Preferences pre = Preferences.systemRoot().node("/register");  
        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("/register");  
        return pre.get(key, "time");  
    }  
  
    /*** 
     * 清除注册表 
     *  
     * @throws BackingStoreException 
     */  
    public void clearValue() throws BackingStoreException {  
        Preferences pre = Preferences.systemRoot().node("/register");  
        pre.clear();  
    }  
    
    public String readValid(){
    	String content = "";
    	String result = "";
        try {    
            Process ps = null;    
            ps = Runtime.getRuntime().exec("reg query " + path + doc);
            ps.getOutputStream().close();    
            InputStreamReader i = new InputStreamReader(ps.getInputStream());    
            String line;    
            BufferedReader ir = new BufferedReader(i);    
            while ((line = ir.readLine()) != null) {    
                content += line;
            }    
            result = content.substring(content.indexOf("[")+1, content.indexOf("]"));
        } catch (IOException e) {    
            e.printStackTrace();     
        } 
        return result;
    }
  
    public static void main(String[] args) {  
        Registery reg = new Registery();  
        reg.writeValue();  
        System.out.println("version========="+reg.getValue("version"));  
    	
//        try {  
//            reg.clearValue();  
//        } catch (BackingStoreException e) {  
//            e.printStackTrace();  
//        }  
          
        //可以读取任意路径下的、  
    	String content = "";
        try {    
            Process ps = null;    
            //ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\javaplayer"); 
            ps = Runtime.getRuntime().exec("reg query HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\register");
            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);
                content += line;
            }    
            System.out.println("========"+content);
            Integer start = content.indexOf("[");
            Integer end = content.indexOf("]");
            System.out.println("----"+start+" "+end);
            String result = content.substring(start+1, end);
            System.out.println("-result--"+result);
        } catch (IOException e) {    
            e.printStackTrace();     
        }    
    }  
    
    
    private String path = "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Prefs\\";
    private String doc = "register";
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值