在Ubuntu系统上,我搜索/media目录,并假设每个文件夹都是一个挂载的文件系统,以获取其大小和信息:
String username = System.getProperty("user.name");
File media = new File("/media/" + username);
System.out.println("Partition: " + media.getAbsolutePath());
File[] fileList = media.listFiles();
if (fileList != null)
for (File f : fileList) {
if (f.isDirectory())
printDiskData(f);
}
void printDiskData(File partitionMountPoint) {
System.out.println(partitionMountPoint.getAbsolutePath());
System.out.println(String.format("Total disk size: %.2f GB", partitionMountPoint.getTotalSpace() / 1073741824f));
System.out.println(String.format("Free disk size: %.2f GB", partitionMountPoint.getFreeSpace() / 1073741824f));
System.out.println(String.format("Usabale disk size: %.2f GB", partitionMountPoint.getUsableSpace() / 1073741824f));
}其中一些文件夹可能不指向已挂载的驱动器,而只是常规文件夹。因此,我需要检测这些文件是否是相同/ (根)分区上的常规文件夹,如果不是,则获取它们的大小、可用空间……
发布于 2020-10-02 18:04:01
这是一种在Java中确定目录路径的顶级挂载点的方法,而无需尝试调用Linux文件或脚本。
public static Path mountOf(Path p) throws IOException {
FileStore fs = Files.getFileStore(p);
Path temp = p.toAbsolutePath();
Path mountp = temp;
while( (temp = temp.getParent()) != null && fs.equals(Files.getFileStore(temp)) ) {
mountp = temp;
}
return mountp;
}它假设父目录的文件存储将在检查文件存储高于已安装文件系统的级别时更改。这可以在Windows10和WSL Ubuntu JDK15上运行--其他版本不确定。
Path p = Path.of("/mnt/c/dev/tools");
Path m = mountOf(p);
System.out.println("Mount point of "+p+" => "+m);打印:
Mount point of /mnt/c/dev/tools => /mnt/c然后,要计算出每个文件系统挂载一次的空闲空间,您只需要为从mountOf(p)返回的每个唯一路径调用printDiskData(m.toFile())。
https://stackoverflow.com/questions/56712909
复制相似问题