Java获取当前系统情况总结
- 硬盘详细1
- 硬盘详细2
- 硬盘详细3
- cpu总使用率
- 内存使用率
- 硬件信息
- io读写状态
硬盘详细1
public static JSONArray files() throws Exception {
DecimalFormat df = new DecimalFormat("0");
JSONArray array = new JSONArray();
// 磁盘使用情况
File[] files = File.listRoots();
for (File file : files) {
JSONObject json = new JSONObject();
// String total = file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024
// + "G";
json.put("盘符", file.toString().replace("\", "/"));
json.put("used",df.format((file.getTotalSpace()-file.getFreeSpace()) / 1024 / 1024/1024) );
json.put("avail",df.format( file.getFreeSpace() / 1024 / 1024/1024 ));
json.put("total",df.format( file.getTotalSpace() / 1024 / 1024/1024 ));
array.add(json);
}
return array;
}
硬盘详细2
public static JSONArray files3() throws Exception {
DecimalFormat df = new DecimalFormat("0.0");
JSONArray array = new JSONArray();
// 磁盘使用情况
File[] files = File.listRoots();
for (File file : files) {
JSONObject json = new JSONObject();
json.put("panfu",file.getPath());
// String total = file.getTotalSpace() * 1.0 / 1024 / 1024 / 1024
// + "G";
json.put("total",df.format(file.getTotalSpace() / 1024 / 1024/1024) );
json.put("used",df.format((file.getTotalSpace()-file.getFreeSpace()) / 1024 / 1024/1024) );
json.put("avail",df.format( file.getFreeSpace() / 1024 / 1024/1024 ));
array.add(json);
}
return array;
}
硬盘详细3
public static JSONArray files2() throws Exception {
DecimalFormat df = new DecimalFormat("0");
JSONArray array = new JSONArray();
Sigar sigar = new Sigar();
FileSystem fslist[] = sigar.getFileSystemList();
for (int i = 0; i < fslist.length; i++) {
FileSystem fs = fslist[i];
switch (fs.getType()) {
case 0: // TYPE_UNKNOWN :未知
break;
case 1: // TYPE_NONE
break;
case 2: // TYPE_LOCAL_DISK : 本地硬盘
FileSystemUsage usage = sigar.getFileSystemUsage(fs.getDirName());
JSONObject json = new JSONObject();
// 文件系统可用大小
float avail = usage.getAvail();
json.put("盘符", fs.getDevName().replace("\", "/"));
json.put("avail", df.format(avail / 1024 / 1024));
// 文件系统已经使用量
float used = usage.getUsed();
json.put("used", df.format(used / 1024 / 1024));
// 硬盘总大小
float total = usage.getTotal();
json.put("total", df.format(total / 1024 / 1024));
array.add(json);
break;
case 3:// TYPE_NETWORK :网络
break;
case 4:// TYPE_RAM_DISK :闪存
break;
case 5:// TYPE_CDROM :光驱
break;
case 6:// TYPE_SWAP :页面交换
break;
}
}
return array;
}
cpu总使用率
public static double cpu() throws SigarException {
double combined = 0;
Sigar sigar = new Sigar();
CpuPerc cpuList[] = sigar.getCpuPercList();
for (int i = 0; i < cpuList.length; i++) {
combined = combined + cpuList[i].getCombined();
}
Double dou = new Double(combined);
if (dou.isNaN()) {
combined = 0.0;
}
java.text.DecimalFormat dFormat = new DecimalFormat("#.##");
String str = dFormat.format(combined / (cpuList.length) * 100);
return Double.parseDouble(str);
}
内存使用率
public static double memory() throws SigarException {
Sigar sigar = new Sigar();
Mem mem = sigar.getMem();
java.text.DecimalFormat dFormat = new DecimalFormat("#.##");
String str = dFormat.format(((double) mem.getUsed()) / ((double) mem.getTotal()) * 100);
return Double.parseDouble(str);
}
硬件信息
public static String getHardwareMsg() throws Exception {
DecimalFormat df = new DecimalFormat("0.0");
float used = 0;
float usable = 0;
String cpu = "";
String memory = "";
JSONArray IOarray = FreeDiskSpace.files();
for (int i = 0; i < IOarray.size(); i++) {
used = used + Float.parseFloat(IOarray.getJSONObject(i).getString("used"));
usable = usable + Float.parseFloat(IOarray.getJSONObject(i).getString("avail"));
}
cpu = df.format(FreeDiskSpace.cpu());// CPU
memory = df.format(FreeDiskSpace.memory());// 内存
JSONObject result = new JSONObject();
result.put("kjAllSize", used + usable);
result.put("kjSySize", used);
result.put("kjKySize", usable);
result.put("cpuSysSyl", cpu + "%");
result.put("memSyl", memory + "%");
return result.toString();
}
io读写状态
public static String io() throws SigarException {
if(cpu()>90){
return "差";
}else {
return "良好";
}
}