JAVA 레지스트리 읽어오기

가이드문구

java 레지스트리 읽어오기

자바 레지스트리 읽어오기 소스 입니다.

  1. import java.io.*;
  2. public class RegQuery {
  3. private static final String REGQUERY_UTIL = "reg query ";
  4. private static final String REGSTR_TOKEN = "REG_SZ";
  5. private static final String REGDWORD_TOKEN = "REG_DWORD";
  6. private static final String PERSONAL_FOLDER_CMD = REGQUERY_UTIL +
  7. "\"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\"
  8. + "Explorer\\Shell Folders\" /v Personal";
  9. private static final String CPU_SPEED_CMD = REGQUERY_UTIL +
  10. "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
  11. + " /v ~MHz";
  12. private static final String CPU_NAME_CMD = REGQUERY_UTIL +
  13. "\"HKLM\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\""
  14. + " /v ProcessorNameString";
  15. public static String getCurrentUserPersonalFolderPath() {
  16. try {
  17. Process process = Runtime.getRuntime().exec(PERSONAL_FOLDER_CMD);
  18. StreamReader reader = new StreamReader(process.getInputStream());
  19. reader.start();
  20. process.waitFor();
  21. reader.join();
  22. String result = reader.getResult();
  23. int p = result.indexOf(REGSTR_TOKEN);
  24. if (p == -1)
  25. return null;
  26. return result.substring(p + REGSTR_TOKEN.length()).trim();
  27. }
  28. catch (Exception e) {
  29. return null;
  30. }
  31. }
  32. public static String getCPUSpeed() {
  33. try {
  34. Process process = Runtime.getRuntime().exec(CPU_SPEED_CMD);
  35. StreamReader reader = new StreamReader(process.getInputStream());
  36. reader.start();
  37. process.waitFor();
  38. reader.join();
  39. String result = reader.getResult();
  40. int p = result.indexOf(REGDWORD_TOKEN);
  41. if (p == -1)
  42. return null;
  43. // CPU speed in Mhz (minus 1) in HEX notation, convert it to DEC
  44. String temp = result.substring(p + REGDWORD_TOKEN.length()).trim();
  45. return Integer.toString
  46. ((Integer.parseInt(temp.substring("0x".length()), 16) + 1));
  47. }
  48. catch (Exception e) {
  49. return null;
  50. }
  51. }
  52. public static String getCPUName() {
  53. try {
  54. Process process = Runtime.getRuntime().exec(CPU_NAME_CMD);
  55. StreamReader reader = new StreamReader(process.getInputStream());
  56. reader.start();
  57. process.waitFor();
  58. reader.join();
  59. String result = reader.getResult();
  60. int p = result.indexOf(REGSTR_TOKEN);
  61. if (p == -1)
  62. return null;
  63. return result.substring(p + REGSTR_TOKEN.length()).trim();
  64. }
  65. catch (Exception e) {
  66. return null;
  67. }
  68. }
  69. static class StreamReader extends Thread {
  70. private InputStream is;
  71. private StringWriter sw;
  72. StreamReader(InputStream is) {
  73. this.is = is;
  74. sw = new StringWriter();
  75. }
  76. public void run() {
  77. try {
  78. int c;
  79. while ((c = is.read()) != -1)
  80. sw.write(c);
  81. }
  82. catch (IOException e) { ; }
  83. }
  84. String getResult() {
  85. return sw.toString();
  86. }
  87. }
  88. public static void main(String s[]) {
  89. System.out.println("Personal directory : "
  90. + getCurrentUserPersonalFolderPath());
  91. System.out.println("CPU Name : " + getCPUName());
  92. System.out.println("CPU Speed : " + getCPUSpeed() + " Mhz");
  93. }
  94. }
작성자 소개
초이 프로필
WrapUp 블로거

초이

반려견을 좋아하고, 차를 좋아하고, 여행을 좋아하고, 맛집을 찾아 즐기는 웹 개발자 입니다^^