java代码硬盘使用比例 java代码硬盘使用比例是多少( 二 )


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);

推荐阅读