Java IO Tutorial

How to get free disk space in Java

In Java old days, it lacks of method to determine the free disk space on a partition. But this is changed since JDK 1.6 released, a few new methods – getTotalSpace(), getUsableSpace() and getFreeSpace(), are bundled with java.io.File to retrieve the partition or disk space detail.

Example


package com.mkyong;

import java.io.File;

public class DiskSpaceDetail
{
    public static void main(String[] args)
    {	
    	File file = new File("c:");
    	long totalSpace = file.getTotalSpace(); //total disk space in bytes.
    	long usableSpace = file.getUsableSpace(); ///unallocated / free disk space in bytes.
    	long freeSpace = file.getFreeSpace(); //unallocated / free disk space in bytes.
    	
    	System.out.println(" === Partition Detail ===");
    	
    	System.out.println(" === bytes ===");
    	System.out.println("Total size : " + totalSpace + " bytes");
    	System.out.println("Space free : " + usableSpace + " bytes");
    	System.out.println("Space free : " + freeSpace + " bytes");
    	
    	System.out.println(" === mega bytes ===");
    	System.out.println("Total size : " + totalSpace /1024 /1024 + " mb");
    	System.out.println("Space free : " + usableSpace /1024 /1024 + " mb");
    	System.out.println("Space free : " + freeSpace /1024 /1024 + " mb");
    }
}

Output

Display the disk space detail in c: partition.


 === Partition Detail ===

 === bytes ===
Total size : 52428795904 bytes
Space free : 33677811712 bytes
Space free : 33677811712 bytes
 === mega bytes ===
Total size : 49999 mb
Space free : 32117 mb
Space free : 32117 mb
Note
Both getFreeSpace() and getUsableSpace() methods are return the same total free disk space of a given partition. But the real different is not clear, even in the java doc. Tell me if you know what’s the different in between.

Reference

http://download.oracle.com/javase/6/docs/api/java/io/File.html

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
16 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
sam
5 years ago

verry helpful.

thanks mr

Deepti
6 years ago

Above code does not work in the scenario if the location doesn’t exist nor it throws any exception.
It just displays 0 🙁

maxcom
13 years ago

Unix filesystem reserves some percent of space for root-user. getFreeSpace() returns total free space available and getUsableSpace() returns amount of free space available to current user.

vbvb
11 years ago
Reply to  mkyong

bvbvbv

PON RADHA KRISHNAN D
9 years ago
Reply to  vbvb

The above three methods to get disk usage is not working in Linux OSes, but it works in windows. Before trying this, i tried using sigar API which works well for windows and not for linux..Any help is much appreciated.

John
6 years ago

This code runs perfect, but only in current login user mode, but if you open cmd prompt in administrator mode, it doesn’t work for network drives

Deepti
6 years ago

this doesnt work if the location is wrong, nor even throws any exception but assigns zero value

Chaibi Alaa
8 years ago

Java reads a space on disk not partition like swap it will be free but not usable.

Ricky
9 years ago

I used these methods to attempt to get the free space on a Linux box from Java. In my tests, using .getUsableSpace() was much close to the output of the linux df command than .getFreeSpace(). So, that’s what I plan to use.

Alok Singh
11 years ago

getUsableSpace() method does a few additional checks, about write permissions and actual space available to the user. Also, on some systems a certain percentage of total disk space is reserved for admin/root users. This can make your checks for space pass incorrectly if you happen to use the getFreeSpace() method

Pankaj Soni
11 years ago

The difference what I understood between getFreeSpace() and getUsableSpace() is
getFreeSpace() will give the actual space that is free in the partition.
getUsableSpace() will give the free space available to the JVM at that time(may be due to user level restrictions or some other restrictions at OS level, JVM will not have the full access to all the Free space in the partition)

ozkr2805
12 years ago

Buen aporte 🙂

???
12 years ago

This is my first time i visit here; I found so many useful stuff in your website especially its discussion! From the a lot of comments on your articles. I guess Im not the only one receiving the many satisfaction right here! keep up a good job.

saikrishna tummalapalli
9 years ago

hi admin,,,
My problem is to get diskspace of unix drives,,,,,,but java program will run on windows platform…
Plz provide the solution