日期:2014-05-17  浏览次数:21126 次

java获取windows系统信息(CPU,内存,文件系统,硬盘大小)

 Java怎么获取windows系统信息,如CPU,内存,文件系统,硬盘大小? java实现这些功能的确有点麻烦,没有C语言方便.java在windows这方还是弱了一点.不过麻烦是麻烦点,针对这些功能还是可以实现了,以下是 自己整理的一些公用方法.与大家分享下.

?

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

?

?// // 获取内存使用率,这个方法有点问题,不没有解决
?// public static String getMemery(){
?// // OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
?// ManagementFactory.getOperatingSystemMXBean();
?// OperatingSystemMXBean osmxb = (OperatingSystemMXBean)
?// ManagementFactory.getMemoryManagerMXBeans();
?// // 总的物理内存+虚拟内存
?// long totalvirtualMemory = osmxb.getTotalSwapSpaceSize();
?// osmxb.
?//?
?// // 剩余的物理内存
?// 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;
?}

?

?// 单位为G
?public static List getDiskToG() {
??// 操作系统
??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.#"); //这个方法是对数字进行转换了,大家可以研究下
????double f1 = (free / (1024.0 * 1024 * 1024)); //free的值是字符类型,所以除以1024(1024字节等于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";
???// 取进程信息
???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("Wri