国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - 使用Java獲取系統信息的常用代碼整理總結

使用Java獲取系統信息的常用代碼整理總結

2020-01-20 11:42chenhua_1984 JAVA教程

這篇文章主要介紹了使用Java獲取系統信息的常用代碼整理總結,在服務器端一般經常能夠用到,歡迎收藏,需要的朋友可以參考下

1.獲取CPU和內存信息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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系統信息(CPU,內存,文件系統)
 * @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());
 }
 //獲取內存使用率
 public static String getMemery(){
 OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
 // 總的物理內存+虛擬內存
 long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
 // 剩余的物理內存
 long freePhysicalMemorySize = osmxb.getFreePhysicalMemorySize();
 Double compare=(Double)(1-freePhysicalMemorySize*1.0/totalvirtualMemory)*100;
 String str="內存已使用:"+compare.intValue()+"%";
 return str;
 }
 //獲取文件系統使用率
 public static List<String> getDisk() {
 // 操作系統
 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+":盤 已使用 "+compare.intValue()+"%";
     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";
       // 取進程信息
       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相關信息
  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;
        }
        // 字段出現順序: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對漢字處理存在問題(把一個漢字視為一個字節),因此在 包含漢字的字符串時存在隱患,現調整如下:
  * @param src 要截取的字符串
  * @param start_idx 開始坐標(包括該坐標)
  * @param end_idx 截止坐標(包括該坐標)
  * @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.獲取本機的IP地址:

?
1
2
3
4
5
private static String getIpAddress() throws UnknownHostException {
    InetAddress address = InetAddress.getLocalHost();
 
    return address.getHostAddress();
  }

 
3.獲得網卡地址

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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.獲得操作系統帳號

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

5.獲得操作系統版本

?
1
2
3
4
5
6
import java.util.Properties; 
   
Properties props=System.getProperties(); //獲得系統屬性集 
String osName = props.getProperty("os.name"); //操作系統名稱 
String osArch = props.getProperty("os.arch"); //操作系統構架 
String osVersion = props.getProperty("os.version"); //操作系統版本

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

java.version    Java 運行時環境版本 
java.vendor     Java 運行時環境供應商 
java.vendor.url     Java 供應商的 URL 
java.home   Java 安裝目錄 
java.vm.specification.version   Java 虛擬機規范版本 
java.vm.specification.vendor    Java 虛擬機規范供應商 
java.vm.specification.name  Java 虛擬機規范名稱 
java.vm.version     Java 虛擬機實現版本 
java.vm.vendor  Java 虛擬機實現供應商 
java.vm.name    Java 虛擬機實現名稱 
java.specification.version  Java 運行時環境規范版本 
java.specification.vendor   Java 運行時環境規范供應商 
java.specification.name     Java 運行時環境規范名稱 
java.class.version  Java 類格式版本號 
java.class.path     Java 類路徑 
java.library.path   加載庫時搜索的路徑列表 
java.io.tmpdir  默認的臨時文件路徑 
java.compiler   要使用的 JIT 編譯器的名稱 
java.ext.dirs   一個或多個擴展目錄的路徑 
os.name     操作系統的名稱 
os.arch     操作系統的架構 
os.version  操作系統的版本 
file.separator  文件分隔符(在 UNIX 系統中是“/”) 
path.separator  路徑分隔符(在 UNIX 系統中是“:”) 
line.separator  行分隔符(在 UNIX 系統中是“/n”) 
user.name   用戶的賬戶名稱 
user.home   用戶的主目錄 
user.dir    用戶的當前工作目錄 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 综合久久亚洲 | 欧美日韩国产一级片 | 中文字幕在线免费观看 | 一区免费视频 | 日韩欧美国产一区二区三区 | 色婷婷一区二区三区 | 日韩欧美成人影院 | 中文字幕日韩av | 一区二区三区在线看 | 懂色一区二区三区av片 | 思热99re视热频这里只精品 | av在线综合网 | 国产成人精品一区二区三区视频 | 国产精品国产精品国产专区不片 | 寡妇高潮免费视频一区二区三区 | 综合久久99| 日韩片一区 | av色资源| 999久久久 | 亚洲午夜视频 | 国产高清在线不卡 | 亚洲电影一区二区 | 国产在线观看 | 亚洲日本va中文字幕 | 久久久亚洲精 | 四影虎影www4hu23cmo | 久久99国产精一区二区三区 | 久久精品国产一区二区三区不卡 | 狠狠干天天爱 | 久久婷婷色| 国内激情自拍 | 亚洲国产精品一区二区久久 | 精品无码久久久久国产 | a∨色狠狠一区二区三区 | porn在线 | 亚洲天堂久久精品 | 九九热这里| 欧美午夜精品久久久久久浪潮 | 99re在线观看视频 | 色天天综合久久久久综合片 | 懂色av中文一区二区三区天美 |