jaav读取注册表

读取的注册表类型仅限于:REG_SZ
private static final String REGQUERY_UTIL = "reg query ";
    private static final String REGSTR_TOKEN = "REG_SZ";
    private static final String REGDWORD_TOKEN = "REG_DWORD";
 
    //java注册表地址
    private static final String REGEDIT_FOLDER_CMD = REGQUERY_UTIL
            + "\"HKLM\\SOFTWARE\\JavaSoft\\Java Development Kit\\1.6.0_27"
            + "\" /v JavaHome";
    //Tomcat注册表地址
    private static final String REGEDIT_TOMCAT_CMD=REGQUERY_UTIL
    		+"\"HKLM\\SOFTWARE\\Apache Software Foundation\\Tomcat\\6.0\\Tomcat6"
    		+"\" /v InstallPath";
    /**
     * 读取注册表 获得java tomcat所对应该的安装目录
     * @param regeditPath 注册表地址
     * @return
     */
    public static String getMyDocumentsFromWinRegistry(String regeditPath) {
        try {
            Process process = Runtime.getRuntime().exec(regeditPath);
            StreamReader streamreader = new StreamReader(process.getInputStream());
 
            streamreader.start();
            process.waitFor();
            streamreader.join();
            String result = streamreader.getResult();
            int p = result.indexOf(REGSTR_TOKEN);
            if (p == -1)
                return null;
            return result.substring(p + REGSTR_TOKEN.length()).trim();
        } catch (Exception e) {
            return null;
        }
    }
    /**
     * 内部类
     * 解决中文乱码问题
     */
    static class StreamReader extends Thread {
        private InputStream is;
        private StringWriter sw;
 
        StreamReader(InputStream is) {
            this.is = is;
            sw = new StringWriter();
        }
        @Override
        public void run() {
            try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
                int c = -1;
                while ((c = is.read()) != -1){
                bos.write(c);
                }                
                sw.write(new String(bos.toString("GBK").getBytes("UTF-8")));
            } catch (IOException e) {
            	e.printStackTrace();
            }
        }
        String getResult() {
            return sw.toString();
        }
    }

你可能感兴趣的:(注册表)