package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class DiskControl {
/**
* 获得磁盘使用率
*/
public static void diskSpace() {
String userRate = "";//使用率
try {
// 读取系统数据库配置文件
InputStream inputStream = DiskControl.class.getClassLoader().getResourceAsStream("default.properties");
Properties p = new Properties();
p.load(inputStream);
// 关闭流
inputStream.close();
// windows系统下
if (System.getProperties().getProperty("os.name").toUpperCase().indexOf("WINDOWS") != -1) {
// System.out.println("当前DIS:window");
String disk = p.getProperty("window.disk").substring(0, 2);//获取当前数据库保存盘
File file = new File(disk);
double totalSpace = file.getTotalSpace();
double freeSpace = file.getFreeSpace();
double usedSpace = totalSpace - freeSpace;
//向上取整,21.1处理后为22
userRate = Math.ceil((usedSpace/totalSpace)*100)+"%";
} else {
// System.out.println("当前DIS:Linux");
userRate = getDeskUsage(p.getProperty("linux.disk"));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取磁盘使用率异常!!");
}
System.out.println("当前磁盘使用率 : " + userRate);
}
// linux服务器通过命令获取磁盘使用率
public static String getDeskUsage(String cmd) throws Exception {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
String userRate = "";//使用率
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
//循环读取Linux窗口显示的内容
while ((str = in.readLine()) != null) {
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0) {
continue;
}
String abc = tmp.toString();
//当循环出**.%时,执行以下处理
if (abc.indexOf("%") != -1) {
if(!(abc.length()>=3?abc.substring(0, 3):abc).toLowerCase().equals("use")){
userRate = abc;
// System.out.println("tmp:"+abc);
break;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
return userRate;
}
/* public static void main(String[] args){
diskSpace();
}*/
//Linux下通过计算获得磁盘总大小及使用大小
/* public void test() throws Exception {
double totalHD = 0;
double usedHD = 0;
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("df -hl /home");// df -hl 查看硬盘空间
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String str = null;
String[] strArray = null;
while ((str = in.readLine()) != null) {
int m = 0;
strArray = str.split(" ");
for (String tmp : strArray) {
if (tmp.trim().length() == 0){
continue;
}
if(tmp.toString().indexOf("%")!=-1){
}
++m;
System.out.println("----tmp1----" + tmp);//循环获得的列名及参数
if (tmp.indexOf("G") != -1) {
if (m == 2) {
System.out.println("---G2----" + tmp);
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
if (m == 3) {
System.out.println("---G3----" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1)) * 1024;
}
}
if (tmp.indexOf("M") != -1) {
if (m == 2) {
System.out.println("---M4---" + tmp);
if (!tmp.equals("") && !tmp.equals("0"))
totalHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
}
if (m == 3) {
System.out.println("---M5---" + tmp);
if (!tmp.equals("none") && !tmp.equals("0"))
usedHD += Double.parseDouble(tmp.substring(0, tmp.length() - 1));
System.out.println("----6----" + usedHD);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
in.close();
}
System.out.println("----totalHD----" + totalHD);//磁盘总大小
System.out.println("----usedHD----" + usedHD);//使用大小
//计算使用率
int d = (int) Math.ceil((usedHD/totalHD)* 100);
}*/
}