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

来源:互联网 发布:daz studio mac下载 编辑:程序博客网 时间:2024/06/05 00:15

通过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";}