How can I get a list of all mounted filesystems in Java on Unix?

后端 未结 6 1033
误落风尘
误落风尘 2020-12-10 19:38

I\'m running Java on a Unix platform. How can I get a list of all mounted filesystems via the Java 1.6 API?

I\'ve tried File.listRoots() but that return

6条回答
  •  北海茫月
    2020-12-10 20:04

    OSHI (Operating System and Hardware Information library for Java) can be useful here: https://github.com/oshi/oshi.

    Check out this code:

    @Test
    public void test() {
    
        final SystemInfo systemInfo = new SystemInfo();
        final OSFileStore[] fileStores = systemInfo.getOperatingSystem().getFileSystem().getFileStores();
        Stream.of(fileStores)
        .peek(fs ->{
            System.out.println("name: "+fs.getName());
            System.out.println("type: "+fs.getType() );
            System.out.println("str: "+fs.toString() );
            System.out.println("mount: "+fs.getMount());
            System.out.println("...");
        }).count();
    
    }
    

提交回复
热议问题