Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取文件夹';s使用java安装点_Java_File_Filesystems - Fatal编程技术网

获取文件夹';s使用java安装点

获取文件夹';s使用java安装点,java,file,filesystems,Java,File,Filesystems,在Ubuntu系统上,我在/media目录中搜索,并假设每个文件夹都是一个挂载的文件系统,以获取其大小和信息: String username = System.getProperty("user.name"); File media = new File("/media/" + username); System.out.println("Partition: " + media.getAbsolutePath()); File[] fileList = media.listFiles();

在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));
    }

有些文件夹可能不指向已装入的驱动器,而只是普通文件夹。因此,我需要检测这些文件是否是同一
/
(根)分区上的常规文件夹,如果不是,则获取它们的大小、可用空间,

这是一种在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;
}
它假设父目录的文件存储将在检查高于已装入文件系统级别的文件存储后发生更改。这适用于Windows 10和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())
。如果您可以通过bash获得所需的结果,您可以尝试2。FileSystems.getDefault().getFileStores()+按“/media/”用户名过滤在Linux或其他使用/proc文件系统的系统上,您可以查看/proc/mounts文件的内容。此文件可以作为文本读取,以获取当前装载点的列表。