Monday, July 6, 2009

Java Source code to extract the disk space status for drives

JDK 1.6 introduces new functionalities to extract the disk space status for a particular drive. Check it out.


The following program will list out the details of the status for a particular drive.

/*
Class to deal with disk spaces
JDK 1.6 Compatible
*/

import java.io.File;
import java.text.DecimalFormat;

public class DiskSpace {
public static void main(String[] args){

File file = new File("D:\\");
DecimalFormat df = new DecimalFormat("##.##");

long totalSpace = file.getTotalSpace();
long usableSpace = file.getUsableSpace();
long freeSpace = file.getFreeSpace();

File drives[] = File.listRoots();

System.out.println("The drives available are: ");

// Loop through the drive list and display the drives.
for (int index = 0; index < drives.length; index++) {
System.out.println(drives[index]);
}

// Convert it to GigaBytes
double valueGBTotalSpace = (double) totalSpace / 1024 / 1024 / 1024;
double valueGBUsableSpace = (double) usableSpace/ 1024 / 1024 / 1024;
double valueGBFreeSpace = (double) freeSpace/ 1024 / 1024 / 1024;
double valueGBUsedSpace = (double) (totalSpace - freeSpace)/ 1024 / 1024 / 1024;



System.out.println("Total space in " + file + " partition is "+df.format(valueGBTotalSpace));
System.out.println("Total Usable space available in " + file + " partition is "+df.format(valueGBUsableSpace));
System.out.println("Total Free space available in " + file + " partition is "+df.format(valueGBFreeSpace));
System.out.println("Total Used space available in " + file + " partition is "+df.format(valueGBUsedSpace));
}
}


Hope you all will find it useful.

1 comment:

Total Pageviews