How to find free disk space using Java

There are several method calls in Java that can be used to get storage-related information for a disk drive. The method to extract this information is declared in the File class, which is in the java.io package. Details on how to call this method and how to use it are given below:

Note: These codes will not run on online ide. Also, they will all work on Java 1.6 and above versions

  1. Java.io.File.getFreeSpace(): Get total free space available in a drive

Syntax:

public long getFreeSpace()
Returns the size of the partition named by this abstract pathname.
Returns:
The size, in bytes, of the partition or 0L if this 
abstract pathname does not name a partition
Throws:
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

 

// Java program to get the amount of free space available on any drive
import java.io.*;

public class Test
{
	public static void main(String[] args)
	{
		File file = new File("E:\\");
		
		double size = file.getFreeSpace() / (1024.0 * 1024 * 1024);
		System.out.printf( "%.3f GB\n", size);	
	}
}

Output:

18.242 GB

2. Java.io.File.getUsableSpace(): Usable shared storage space is available with the device. Returns the number of bytes available for this partition virtual machine named with this abstract path name. Whenever possible, this method checks for write permissions and other operating system limitations, so it usually provides a more accurate estimate of how much new data can actually be written than getFreeSpace().
The number of bytes returned is an indication, but not a guarantee that most or some of these bytes can be used. The number of unallocated bytes will most likely be correct immediately after this call. Possibly inaccurately performed by external I/O, including those performed on systems outside of this virtual machine. This method does not guarantee that writing to this file system will be successful.

Syntax:

public long getUsableSpace()
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().
Throws:
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

 

import java.io.*;

public class Test
{
	public static void main(String[] args)
	{
		double size =
			new File("C:\\").getUsableSpace() / (1024.0 * 1024 * 1024);
		System.out.printf( "%.3f GB\n", size);	
	}
}

Output:

62.857 GB

3. Java.io.File.getTotalSpace(): Total drive capacity. This method returns the size of the partition called from this abstract path name.

Syntax:

public long getTotalSpace()
Returns: Returns:
The size, in bytes, of the partition or 0L if this abstract pathname 
does not name a partition
Throws:
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

 

import java.io.*;

public class Test
{
	public static void main(String[] args)
	{
		double size =
			new File("C:\\").getUsableSpace() / (1024.0 * 1024 * 1024);
		System.out.printf( "%.3f GB\n", size);	
	}
}

Output:

62.857 GB

 

Submit Your Programming Assignment Details