Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法

1.利用jdk自带的API获取信息:(只支持jdk1.60以上的版本啊)

 

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;

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 getDisk() {
  // 操作系统
  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;
 }

 //获得cpu使用率
 public static String getCpuRatioForWindows() {
         try {
             String procCmd = System.getenv("windir") + "//system32//wbem//wmic.exeprocess 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;
           

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值