import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class T {
private static final int DRIVE_TYPE_HARD = 3;
private static final String CHARSET = "GBK";
private static final String CAP_LOAD_PERCENTAGE = "LoadPercentage";
private static final String CAP_CAPACITY = "Capacity";
private static final String CAP_CAPTION = "Caption";
private static final String CAP_DRIVE_LETTER = "DriveLetter";
private static final String CAP_DRIVE_TYPE = "DriveType";
private static final String CAP_FREE_SPACE = "FreeSpace";
private static final ListString CAPS_VOLUME = Arrays.asList(CAP_CAPACITY,
CAP_CAPTION, CAP_DRIVE_LETTER, CAP_DRIVE_TYPE, CAP_FREE_SPACE);
private static final String CMD_CPU = "wmic cpu get " + CAP_LOAD_PERCENTAGE;
private static final String CMD_VOLUME = "wmic volume get " + CAP_CAPACITY
+ "," + CAP_CAPTION + "," + CAP_DRIVE_LETTER + "," + CAP_DRIVE_TYPE + ","
+ CAP_FREE_SPACE;
public static void main(String[] args) throws IOException {
printDiskUsages(getDiskUsages());
printCpuUsage(getCpuLoadPercentage());
}
private static void printDiskUsages(ListDiskUsage diskUsages) {
for (DiskUsage diskUsage : diskUsages) {
System.out.printf("%s占用率java代码硬盘使用比例:%.2f%%\n", diskUsage.caption, diskUsage.usage);
}
}
private static void printCpuUsage(double cpuLoadPercentage) {
System.out.printf("CPU占用率java代码硬盘使用比例:%.2f%%\n", cpuLoadPercentage);
}
/**
* 取得 CPU 占用率 。
*
* @return
* @throws IOException
*/
private static double getCpuLoadPercentage() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_CPU);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
String percentageLine = br.readLine();
if (percentageLine == null) {
return 0;
}
return Double.parseDouble(percentageLine.trim());
}
/**
* 取得硬盘占用率 。
*
* @return
* @throws IOException
*/
private static ListDiskUsage getDiskUsages() throws IOException {
Process process = Runtime.getRuntime().exec(CMD_VOLUME);
InputStream is = process.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, CHARSET));
String captionLine = br.readLine(); // 舍弃标题行
br.readLine(); // 舍弃标题行下空行
MapString, Integer captionToIndex = parseVolumeCaptionLine(captionLine);
String line;
ListDiskUsage result = new ArrayListDiskUsage();
while ((line = br.readLine()) != null) {
if (line.length() == 0) {
// 舍弃空行
continue;
}
DiskUsage diskUsage = parseVolumeLine(line, captionToIndex);
if (diskUsage != null) {
result.add(diskUsage);
}
}
Collections.sort(result, new DiskUsageComparator());
return result;
}
private static MapString, Integer parseVolumeCaptionLine(String captionLine) {
MapString, Integer captionToIndex = new HashMapString, Integer();
for (String caption : CAPS_VOLUME) {
captionToIndex.put(caption, captionLine.indexOf(caption));
}
return captionToIndex;
}
private static DiskUsage parseVolumeLine(String line,
MapString, Integer captionToIndex) {
int driveType = parseVolumeDriveType(line, captionToIndex);
if (driveType != DRIVE_TYPE_HARD) {
return null;
}
String driveLetter = parseVolumeDriveLetter(line, captionToIndex);
if (!isValidDriveLetter(driveLetter)) {
return null;
}
String caption = parseVolumeCaption(line, captionToIndex);
推荐阅读
- 金盘net图书馆集成管理系统操作,金盘图书软件使用说明
- c语言如何添加新节点,c语言如何新建
- excel怎么引用行,excel引用行数据
- fly斗鱼直播房间,斗鱼infi直播间
- java代码不打空格 java代码空格有要求吗
- word转pdf,word转pdf图片缺失怎么办
- linux命令行别名 linux别名文件
- 广元安卓怎么改密码手机,安卓如何更改密码
- 咖啡经营游戏韩国,韩国咖啡店游戏