On this document we will be showing a java example on how to use the getUsableSpace() method of File Class. This method returns the number of bytes available to this virtual machine on the partition named by this abstract pathname. When possible, this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace().

The returned number of available bytes is a hint, but not a guarantee, that it is possible to use most or any of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine. This method makes no guarantee that write operations to this file system will succeed.


  • SecurityException – If a security manager has been installed and it denies RuntimePermission(“getFileSystemAttributes”) or its SecurityManager.checkRead(String) method denies read access to the file named by this abstract pathname.

Method Syntax

public long getUsableSpace()

Method Argument

Data Type Parameter Description

Method Returns

This method returns the number of available bytes on the partition or 0L if the abstract pathname does not name a partition. On systems where this information is not available, this method will be equivalent to a call to getFreeSpace().


Requires Java 1.6 and up

Java File getUsableSpace() Example

Below is a java code demonstrates the use of getUsableSpace() method of File class. The example presented might be simple however it shows the behaviour of the getUsableSpace() method of File class. To make the print outs of the usable space available we made some calculations to print out the equivalent in Kb, Mb and Gb.



 * This example source code demonstrates the use of  
 * getUsableSpace() method of File class.

public class FileGetUsableSpaceExample {

	public static void main(String[] args) {

		// initialize File object
		File file = new File("C:");

		// get the Usable space available
		Long space = file.getUsableSpace();
		// convert into more readable format
		double spaceKb = space/1000;
		double spaceMb = space/1000000;
		double spaceGb = space/1000000000;
		System.out.println("Usable Space in Kb:" + spaceKb +" Kb.");
		System.out.println("Usable Space in Mb:" + spaceMb +" Mb.");
		System.out.println("Usable Space in Gb:" + spaceGb +" Gb.");

Sample Output

Below is the sample output when you run the above example.

java lang File getUsableSpace() example output