您可以尝试使用以下方法解决问题:
我的代码
public List<String> getHDDPartitions() {
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/mounts"), "UTF-8"));
String response;
StringBuilder stringBuilder = new StringBuilder();
while ((response = bufferedReader.readLine()) != null) {
stringBuilder.append(response.replaceAll(" +", "\t") + "\n");
}
bufferedReader.close();
return Lists.newArrayList(Arrays.asList(stringBuilder.toString().split("\n")));
} catch (IOException e) {
LOGGER.error("{}", ExceptionWriter.INSTANCE.getStackTrace(e));
}
return null;
}
public List<Map<String, String>> getMapMounts() {
List<Map<String, String>> resultList = Lists.newArrayList();
for (String mountPoint : getHDDPartitions()) {
Map<String, String> result = Maps.newHashMap();
String[] mount = mountPoint.split("\t");
result.put("FileSystem", mount[2]);
result.put("MountPoint", mount[1]);
result.put("Permissions", mount[3]);
result.put("User", mount[4]);
result.put("Group", mount[5]);
result.put("Total", String.valueOf(new File(mount[1]).getTotalSpace()));
result.put("Free", String.valueOf(new File(mount[1]).getFreeSpace()));
result.put("Used", String.valueOf(new File(mount[1]).getTotalSpace() - new File(mount[1]).getFreeSpace()));
result.put("Free Percent", String.valueOf(getFreeSpacePercent(new File(mount[1]).getTotalSpace(), new File(mount[1]).getFreeSpace())));
resultList.add(result);
}
return resultList;
}
private Integer getFreeSpacePercent(long total, long free) {
Double result = (Double.longBitsToDouble(free) / Double.longBitsToDouble(total)) * 100;
return result.intValue();
}