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

后端 未结 6 1090
误落风尘
误落风尘 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 19:48

    In Java7+ you can use nio

    import java.io.IOException;
    import java.nio.file.FileStore;
    import java.nio.file.FileSystems;
    
    public class ListMountedVolumesWithNio {
       public static void main(String[] args) throws IOException {
          for (FileStore store : FileSystems.getDefault().getFileStores()) {
             long total = store.getTotalSpace() / 1024;
             long used = (store.getTotalSpace() - store.getUnallocatedSpace()) / 1024;
             long avail = store.getUsableSpace() / 1024;
             System.out.format("%-20s %12d %12d %12d%n", store, total, used, avail);
          }
       }
    }
    

提交回复
热议问题