Showing posts with label Percent free space on disk. Show all posts
Showing posts with label Percent free space on disk. Show all posts

Thursday 26 February 2015

How to find disk space utilization using Java?

 import java.io.File;  
 import java.nio.file.FileSystem;  
 import java.nio.file.FileSystems;  
 import java.nio.file.Path;  
 import java.util.Iterator;  
 public class DiskSpace {  
  public static void main(String[] args) {  
            FileSystem fs = FileSystems.getDefault();  
            Iterable drives = fs.getRootDirectories();  
            Iterator path = drives.iterator();  
            while(path.hasNext()){  
                  Path p = path.next();    
                  String drive = p.toString();  
                  System.out.println(drive);  
                  File f = new File(drive);  
                  float freeSpace = f.getFreeSpace();  
                  float totalSpace = f.getTotalSpace();  
                  if(totalSpace > 0){  
                          float percentFree = (freeSpace/totalSpace)*100;  
                          System.out.println(drive + "---- " +freeSpace + "------ " + totalSpace + "---------- " + percentFree);  
                  }  
            }  
      }  
 }