In this section we will be showing a java source code to check disk space on a specified partition. Both the total disk space available and total disk space will be shown on this source code. The idea is to check the disk space using the File class. The abstract filename declared on the File constructor will be the basis of what is the partition to be checked.

package com.teknoscope.tutorial;

import java.io.File;

public class GetDiskSpace {

 /**
 * This sample source code shows
 * how to check the disk space of a partition using java
 * Property of teknoscope.com
 * All Rights Reserved
 * Version 1.0
 * 08/25/2012
 */
 public static void main(String[] args) {
 File f = new File("C:\\");
 System.out.println("Printing the total space");
 System.out.println(f.getTotalSpace() +" bytes");
 System.out.println(f.getTotalSpace()/1000.00 +" Kilobytes");
 System.out.println(f.getTotalSpace()/1000000.00 +" Megabytes");
 System.out.println(f.getTotalSpace()/1000000000.00 +" Gigabytes");
 System.out.println("----------------------------");
 System.out.println("Printing the free space");
 System.out.println(f.getFreeSpace() +" bytes");
 System.out.println(f.getFreeSpace()/1000.00 +" Kilobytes");
 System.out.println(f.getFreeSpace()/1000000.00 +" Megabytes");
 System.out.println(f.getFreeSpace()/1000000000.00 +" Gigabytes");
 }

}

Running the source code above on my machine produces the following output:

Printing the total space
320070479872 bytes
3.20070479872E8 Kilobytes
320070.479872 Megabytes
320.070479872 Gigabytes
----------------------------
Printing the free space
271363801088 bytes
2.71363801088E8 Kilobytes
271363.801088 Megabytes
271.363801088 Gigabytes