Java FileStore get total space and free space

Description

Java FileStore get total space and free space

import java.nio.file.FileStore;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {

  public static void main(String[] argv) throws Exception {
    // get the current file storage pool
    Path current = Paths.get("");
    FileStore store = Files.getFileStore(current);
    // find the free storage space
    long totalSpace = store.getTotalSpace();
    long freeSpace = store.getUsableSpace();
    // get this as a percentage (with two digits)
    double percent = (double) freeSpace / (double) totalSpace * 100;
    percent = (int) (percent * 100) / (double) 100;
    // set the label's text
    System.out.println(freeSpace + " free out of " + totalSpace + " (" + percent + "%)");
  }//  w  w  w. j  a  v a2  s  .  c om

}



PreviousNext

Related