问题遇到的现象和发生背景
如何获取磁盘分区数量和容量
您好,可以使用如下代码试试:
public static void main(String[] args) {
File[] roots = File.listRoots();// 获取磁盘分区列表
for (File file : roots) {
String free = "空闲未使用 = " + file.getFreeSpace() / 1024 / 1024 / 1024 + "G";
String used = "已经使用 = " + file.getUsableSpace() / 1024 / 1024 / 1024 + "G";
String total = "总容量 = " + file.getTotalSpace() / 1024 / 1024 / 1024 + "G";
System.out.println(file.getPath() + "信息如下:");
System.out.println(free + "\t" + used +"\t" + total);
System.out.println("=============================================================");
}
}
打印效果如下: