最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Java獲取系統(tǒng)信息的常用代碼整理總結(jié)

 更新時(shí)間:2015年11月17日 15:30:32   作者:chenhua_1984  
這篇文章主要介紹了使用Java獲取系統(tǒng)信息的常用代碼整理總結(jié),在服務(wù)器端一般經(jīng)常能夠用到,歡迎收藏,需要的朋友可以參考下

1.獲取CPU和內(nèi)存信息

import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import mytools.com.sun.management.OperatingSystemMXBean;
import mytools.java.io.File;
import mytools.java.lang.management.ManagementFactory;
/**
 * 獲取windows系統(tǒng)信息(CPU,內(nèi)存,文件系統(tǒng))
 * @author libing
 *
 */
public class WindowsInfoUtil {
  private static final int CPUTIME = 500;
  private static final int PERCENT = 100;
  private static final int FAULTLENGTH = 10;
  public static void main(String[] args) {
  System.out.println(getCpuRatioForWindows());
  System.out.println(getMemery());
  System.out.println(getDisk());
 }
 //獲取內(nèi)存使用率
 public static String getMemery(){
 OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
 // 總的物理內(nèi)存+虛擬內(nèi)存
 long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
 // 剩余的物理內(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<String> getDisk() {
 // 操作系統(tǒng)
 List<String> list=new ArrayList<String>();
 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+":盤(pán) 已使用 "+compare.intValue()+"%";
     list.add(str);
     }
   }
    return list;
 }
 //獲得cpu使用率
 public static String getCpuRatioForWindows() {
     try {
       String procCmd = System.getenv("windir") + "http://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對(duì)漢字處理存在問(wèn)題(把一個(gè)漢字視為一個(gè)字節(jié)),因此在 包含漢字的字符串時(shí)存在隱患,現(xiàn)調(diào)整如下:
  * @param src 要截取的字符串
  * @param start_idx 開(kāi)始坐標(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;
 }
}

2.獲取本機(jī)的IP地址:

private static String getIpAddress() throws UnknownHostException { 
    InetAddress address = InetAddress.getLocalHost(); 
 
    return address.getHostAddress(); 
  }

 
3.獲得網(wǎng)卡地址

public static String getMACAddress(){ 
 
    String address = ""; 
 
    String os = System.getProperty("os.name"); 
    String osUser=System.getProperty("user.name"); 
    if (os != null && os.startsWith("Windows")) { 
 
      try { 
 
        String command = "cmd.exe /c ipconfig /all"; 
         
        Process p = Runtime.getRuntime().exec(command); 
 
        BufferedReader br =new BufferedReader(new InputStreamReader(p.getInputStream())); 
 
        String line; 
 
        while ((line = br.readLine()) != null) { 
 
          if (line.indexOf("Physical Address") > 0) { 
 
            int index = line.indexOf(":"); 
 
            index += 2; 
 
            address = line.substring(index); 
 
            break; 
 
          } 
 
        } 
 
        br.close(); 
 
        return address.trim(); 
 
      } 
 
      catch (IOException e) { 
      } 
 
    } 
    return address; 
 
  } 

4.獲得操作系統(tǒng)帳號(hào)

String osUser=System.getProperty("user.name"); 

5.獲得操作系統(tǒng)版本

import java.util.Properties;  
   
Properties props=System.getProperties(); //獲得系統(tǒng)屬性集  
String osName = props.getProperty("os.name"); //操作系統(tǒng)名稱(chēng)  
String osArch = props.getProperty("os.arch"); //操作系統(tǒng)構(gòu)架  
String osVersion = props.getProperty("os.version"); //操作系統(tǒng)版本

   
6.一些常用的信息獲得方式整理

java.version    Java 運(yùn)行時(shí)環(huán)境版本 
java.vendor     Java 運(yùn)行時(shí)環(huán)境供應(yīng)商 
java.vendor.url     Java 供應(yīng)商的 URL 
java.home   Java 安裝目錄 
java.vm.specification.version   Java 虛擬機(jī)規(guī)范版本 
java.vm.specification.vendor    Java 虛擬機(jī)規(guī)范供應(yīng)商 
java.vm.specification.name  Java 虛擬機(jī)規(guī)范名稱(chēng) 
java.vm.version     Java 虛擬機(jī)實(shí)現(xiàn)版本 
java.vm.vendor  Java 虛擬機(jī)實(shí)現(xiàn)供應(yīng)商 
java.vm.name    Java 虛擬機(jī)實(shí)現(xiàn)名稱(chēng) 
java.specification.version  Java 運(yùn)行時(shí)環(huán)境規(guī)范版本 
java.specification.vendor   Java 運(yùn)行時(shí)環(huán)境規(guī)范供應(yīng)商 
java.specification.name     Java 運(yùn)行時(shí)環(huán)境規(guī)范名稱(chēng) 
java.class.version  Java 類(lèi)格式版本號(hào) 
java.class.path     Java 類(lèi)路徑 
java.library.path   加載庫(kù)時(shí)搜索的路徑列表 
java.io.tmpdir  默認(rèn)的臨時(shí)文件路徑 
java.compiler   要使用的 JIT 編譯器的名稱(chēng) 
java.ext.dirs   一個(gè)或多個(gè)擴(kuò)展目錄的路徑 
os.name     操作系統(tǒng)的名稱(chēng) 
os.arch     操作系統(tǒng)的架構(gòu) 
os.version  操作系統(tǒng)的版本 
file.separator  文件分隔符(在 UNIX 系統(tǒng)中是“/”) 
path.separator  路徑分隔符(在 UNIX 系統(tǒng)中是“:”) 
line.separator  行分隔符(在 UNIX 系統(tǒng)中是“/n”) 
user.name   用戶(hù)的賬戶(hù)名稱(chēng) 
user.home   用戶(hù)的主目錄 
user.dir    用戶(hù)的當(dāng)前工作目錄 

相關(guān)文章

最新評(píng)論

双鸭山市| 鹿泉市| 始兴县| 绥棱县| 新津县| 香港| 青田县| 班玛县| 化德县| 太湖县| 沧源| 绥江县| 绵阳市| 鄂州市| 九江市| 饶阳县| 府谷县| 栾川县| 同心县| 鹰潭市| 什邡市| 棋牌| 陇川县| 长治县| 黎平县| 阜阳市| 资源县| 蕲春县| 博客| 武穴市| 余庆县| 肥东县| 蒙阴县| 韩城市| 米泉市| 秭归县| 建水县| 大埔区| 山东省| 兴文县| 沅陵县|