Getting FileStore information - Java File Path IO

Java examples for File Path IO:File System

Introduction

A file storage mechanism may be a device, such as a C drive, a partition of a drive, a volume, or some other way of organizing a filesystem's space.

Demo Code

import java.io.IOException;
import java.nio.file.FileStore;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.text.NumberFormat;

public class Main {
  public static void main(String[] args) throws IOException {
    long kiloByte = 1024;
    String format = "%-16s %-20s %-8s %-8s %12s %12s %12s\n";
    System.out.printf(format, "Name", "Filesystem", "Type", "Readonly",
        "Size(KB)", "Used(KB)", "Available(KB)");
    FileSystem fileSystem = FileSystems.getDefault();
    for (FileStore fileStore : fileSystem.getFileStores()) {
      try {/*from   w  w  w  .j a v  a 2  s. com*/
        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();

        NumberFormat numberFormat = NumberFormat.getInstance();
        System.out.printf(format, name, fileStore, type, readOnly,
            numberFormat.format(totalSpace), numberFormat.format(usedSpace),
            numberFormat.format(usableSpace));
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }

}

Related Tutorials