Sample Programs

Sample java program to get disk space - both total and free space

 package org.jusfortechies.example; 

 import java.io.File;
 
 public class GetDiskSpace {

   public static void main(String[] args) {
      
      // Get the total and free space for any given drive  
      File file = new File("C:"); 

      // Get total disk space in mega bytes.   
      long totalSpace = file.getTotalSpace() / (1024 * 1024); 

      // Get the free disk space in mega bytes. 
      long freeSpace = file.getFreeSpace() / (1024 * 1024); 

      System.out.println("Total Disk Space = " + totalSpace + " MB"); 
      System.out.println("Free Disk Space = " + freeSpace+ " MB");
      
   }

 }




blog comments powered by Disqus