Java获取当前系统情况总结

这篇博客介绍了如何使用Java获取系统的硬盘使用情况、CPU总使用率、内存使用率以及I/O读写状态。通过`File`类和`Sigar`库,可以详细地获取每个磁盘分区的使用信息,并计算出整体的资源利用率。此外,还提供了判断I/O状态是否良好的简单方法。
摘要由CSDN通过智能技术生成

硬盘详细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总使用率

	/**
	 * 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读写状态

	/**
	 * io读写状态
	 */
	public static String io() throws SigarException {
		if(cpu()>90){
			return "差";
		}else {
			return "良好";
		}
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值