How do I determine the free space on a drive?
Author: Deron Eriksson
Description: This Java tutorial describes how to get the remaining free space on a drive using Commons IO.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


The freeSpaceKb() method of the FileSystemUtils class of the ApacheSW Commons IOS library is a great way to determine the remaining free space on a hard drive. The GetDriveFreeSpace class demonstrates this.

GetDriveFreeSpace.java

package test;

import java.io.IOException;

import org.apache.commons.io.FileSystemUtils;

public class GetDriveFreeSpace {

	public static void main(String[] args) throws IOException {
		long freeSpaceKb = FileSystemUtils.freeSpaceKb("D:");
		System.out.println("Free space on D (in KB): " + freeSpaceKb);
	}

}

Executing the GetDriveFreeSpace class displays the following results about the space remaining on my D: drive.

Free space on D (in KB): 1227676

If I check these results by examining the properties of my D: drive, I can see that the free space reported by FileSystemUtils.freeSpaceKb() does indeed match the expected results.

D: Drive Properties

Related Tutorials: