乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 獲取windows系統(tǒng)信息(CPU,內(nèi)存,文件系統(tǒng),硬盤大?。?/span>

       CevenCheng 2011-03-11

        Java怎么獲取windows系統(tǒng)信息,如CPU,內(nèi)存,文件系統(tǒng),硬盤大小? java實(shí)現(xiàn)這些功能的確有點(diǎn)麻煩,沒有C語言方便.java在windows這方還是弱了一點(diǎn).不過麻煩是麻煩點(diǎn),針對這些功能還是可以實(shí)現(xiàn)了,以下是自己整理的一些公用方法.與大家分享下.

       

       private static final int CPUTIME = 500;
       private static final int PERCENT = 100;
       private static final int FAULTLENGTH = 10;

       

       // // 獲取內(nèi)存使用率,這個(gè)方法有點(diǎn)問題,不沒有解決
       // public static String getMemery(){
       // // OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
       // ManagementFactory.getOperatingSystemMXBean();
       // OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
       // ManagementFactory.getMemoryManagerMXBeans();
       // // 總的物理內(nèi)存+虛擬內(nèi)存
       // long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
       // osmxb.
       //  
       // // 剩余的物理內(nèi)存
       // long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
       // Double
       // compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
       // String str="內(nèi)存已使用:"+compare.intValue()+"%";
       // return str;
       // }

       // 獲取文件系統(tǒng)使用率
       public static List getDisk() {
        // 操作系統(tǒng)
        List list = new ArrayList();
        for (char c = 'A'; c <= 'Z'; c++) {
         String dirName = c + ":/";
         File win = new File(dirName);
         if (win.exists()) {
          long total = (long) win.getTotalSpace();
          long free = (long) win.getFreeSpace();
          Double compare = (Double) (1 - free * 1.0 / total) * 100;
          String str = c + ":盤  已使用 " + compare.intValue() + "%";
          list.add(str);
         }
        }
        return list;
       }

       

       // 單位為G
       public static List getDiskToG() {
        // 操作系統(tǒng)
        List list = new ArrayList();
        for (char c = 'A'; c <= 'Z'; c++) {
         String dirName = c + ":/";
         File win = new File(dirName);
         if (win.exists()) {
          long total = (long) win.getTotalSpace();
          long free = (long) win.getFreeSpace();
          DecimalFormat df2 = new DecimalFormat("###0.#"); //這個(gè)方法是對數(shù)字進(jìn)行轉(zhuǎn)換了,大家可以研究下
          double f1 = (free / (1024.0 * 1024 * 1024)); //free的值是字符類型,所以除以1024(1024字節(jié)等于1M)
          String str = c + "盤 可用空間" + df2.format(f1) + "G";
          list.add(str);
         }
        }
        return list;
       }

       

      // 獲得cpu使用率
       public static String getCpuRatioForWindows() {
        try {
         String procCmd = System.getenv("windir")
           + "
      \\system32\\wbem\\wmic.exe process get Caption,CommandLine,KernelModeTime,ReadOperationCount,

      ThreadCount,UserModeTime,WriteOperationCount";
         // 取進(jìn)程信息
         long[] c0 = readCpu(Runtime.getRuntime().exec(procCmd));
         Thread.sleep(CPUTIME);
         long[] c1 = readCpu(Runtime.getRuntime().exec(procCmd));
         if (c0 != null && c1 != null) {
          long idletime = c1[0] - c0[0];
          long busytime = c1[1] - c0[1];
          return "CPU使用率:"
            + Double.valueOf(
              PERCENT * (busytime) * 1.0
                / (busytime + idletime)).intValue()
            + "%";
         } else {
          return "CPU使用率:" + 0 + "%";
         }
        } catch (Exception ex) {
         ex.printStackTrace();
         return "CPU使用率:" + 0 + "%";
        }
       }

       

       

      // 讀取cpu相關(guān)信息
       private static long[] readCpu(final Process proc) {
        long[] retn = new long[2];
        try {
         proc.getOutputStream().close();
         InputStreamReader ir = new InputStreamReader(proc.getInputStream());
         LineNumberReader input = new LineNumberReader(ir);
         String line = input.readLine();
         if (line == null || line.length() < FAULTLENGTH) {
          return null;
         }
         int capidx = line.indexOf("Caption");
         int cmdidx = line.indexOf("CommandLine");
         int rocidx = line.indexOf("ReadOperationCount");
         int umtidx = line.indexOf("UserModeTime");
         int kmtidx = line.indexOf("KernelModeTime");
         int wocidx = line.indexOf("WriteOperationCount");
         long idletime = 0;
         long kneltime = 0;
         long usertime = 0;
         while ((line = input.readLine()) != null) {
          if (line.length() < wocidx) {
           continue;
          }
          // 字段出現(xiàn)順序:Caption,CommandLine,KernelModeTime,ReadOperationCount,
          // ThreadCount,UserModeTime,WriteOperation
          String caption = substring(line, capidx, cmdidx - 1).trim();
          String cmd = substring(line, cmdidx, kmtidx - 1).trim();
          if (cmd.indexOf("wmic.exe") >= 0) {
           continue;
          }
          String s1 = substring(line, kmtidx, rocidx - 1).trim();
          String s2 = substring(line, umtidx, wocidx - 1).trim();
          if (caption.equals("System Idle Process")
            || caption.equals("System")) {
           if (s1.length() > 0)
            idletime += Long.valueOf(s1).longValue();
           if (s2.length() > 0)
            idletime += Long.valueOf(s2).longValue();
           continue;
          }
          if (s1.length() > 0)
           kneltime += Long.valueOf(s1).longValue();
          if (s2.length() > 0)
           usertime += Long.valueOf(s2).longValue();
         }
         retn[0] = idletime;
         retn[1] = kneltime + usertime;
         return retn;
        } catch (Exception ex) {
         ex.printStackTrace();
        } finally {
         try {
          proc.getInputStream().close();
         } catch (Exception e) {
          e.printStackTrace();
         }
        }
        return null;
       }

       

      /**
        * 由于String.subString對漢字處理存在問題(把一個(gè)漢字視為一個(gè)字節(jié)),因此在 包含漢字的字符串時(shí)存在隱患,現(xiàn)調(diào)整如下:
        * 
        * @param src
        *            要截取的字符串
        * @param start_idx
        *            開始坐標(biāo)(包括該坐標(biāo))
        * @param end_idx
        *            截止坐標(biāo)(包括該坐標(biāo))
        * @return
        */
       private static String substring(String src, int start_idx, int end_idx) {
        byte[] b = src.getBytes();
        String tgt = "";
        for (int i = start_idx; i <= end_idx; i++) {
         tgt += (char) b[i];
        }
        return tgt;
       }

      測試下:

       public static void main(String[] args) {
        System.out.println(getCpuRatioForWindows());
        // System.out.println(getMemery());
        System.out.println(getDisk());
        System.out.println(getDiskToG());

       }

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多