我想要使用Java程序的Linux系统的分区名称及其总空间,已用空间和可用空间。
对于Windows系统,我得到的是正确的值,但是在Linux中,我仅得到一个驱动器信息:
到目前为止,这是我尝试过的。
public class DiskSpace {
public static void main(String[] args) {
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] drives = File.listRoots();
if (drives != null && drives.length > 0) {
for (File aDrive : drives) {
System.out.println("Drive Letter: " + aDrive);
System.out.println("\tType: " + fsv.getSystemTypeDescription(aDrive));
System.out.println("\tTotal space: " + aDrive.getTotalSpace());
System.out.println("\tFree space: " + aDrive.getFreeSpace());
System.out.println();
}
}
}
Run Code Online (Sandbox Code Playgroud)
Linux上没有驱动器号。如果您想知道有哪些分区以及它们的安装位置,请阅读/proc/mounts
。当有挂载点时(/ proc / mounts中的第二列),用于new File(mountpoint).getTotalSpace()
获取总空间。