Java I/O How to - Get FileStore information








Question

We would like to know how to get FileStore information.

Answer

import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
//  w w w.j a  va  2  s  . c  om
public class Main {
  static final long kiloByte = 1024;

  public static void main(String[] args) throws Exception {
    FileSystem fileSystem = FileSystems.getDefault();

    for (FileStore fileStore : fileSystem.getFileStores()) {
      long totalSpace = fileStore.getTotalSpace() / kiloByte;
      long usedSpace = (fileStore.getTotalSpace() - fileStore
          .getUnallocatedSpace()) / kiloByte;
      long usableSpace = fileStore.getUsableSpace() / kiloByte;
      String name = fileStore.name();
      String type = fileStore.type();
      boolean readOnly = fileStore.isReadOnly();
    }
  }
}