您的位置:首页 > 编程语言 > Java开发

java 获取注册表键值

2015-11-17 23:32 465 查看
    /**

     *

     * @param location path in the registry

     * @param key registry key

     * @return registry value or null if not found

     */

public static final String readRegistry(String location, String key)

{

  try

  {

      // Run reg query, then read output with StreamReader (internal class)

      Process process = Runtime.getRuntime().exec("reg query " +

              '"'+ location + "\" /v " + key);

      InputStream is = process.getInputStream();

      StringBuilder sw = new StringBuilder();

      try

      {

         int c;

         while ((c = is.read()) != -1)

             sw.append((char)c);

      }

      catch (IOException e)

      {

      }

      String output = sw.toString();

      // Output has the following format:

      // \n<Version information>\n\n<key>    <registry type>    <value>\r\n\r\n

      int i = output.indexOf("REG_SZ");

      if (i == -1)

      {

          return null;

      }

      sw = new StringBuilder();

      i += 6; // skip REG_SZ

      // skip spaces or tabs

      for (;;)

      {

         if (i > output.length())

             break;

         char c = output.charAt(i);

         if (c != ' ' && c != '\t')

             break;

         ++i;

      }

      // take everything until end of line

      for (;;)

      {

         if (i > output.length())

             break;

         char c = output.charAt(i);

         if (c == '\r' || c == '\n')

             break;

         sw.append(c);

         ++i;

      }

      return sw.toString();

  }

  catch (Exception e)

  {

      return null;

  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: