Java file get free space


 

Java file get free space

In this section, you will learn how to find the free space of any file or directory.

In this section, you will learn how to find the free space of any file or directory.

Java file get free space

In this section, you will learn how to find the free space of any file or directory.

Description of code:

JDK 1.6 provides few new methods ? getTotalSpace(), getUsableSpace() and getFreeSpace(), bundled with java.io.File. These methods provides essential information regarding disk space.

In the given example, we have created an instance of a File to represent a disc of our file system. Then we have called the method getFreeSpace() of class File through the object of File class.

getFreeSpace() method- This method returns the number of unallocated bytes in the disc.

Here is the code:

import java.io.File;

public class FileFreeSpace {
	public static void main(String[] args) {
		File file = new File("C:");
		long value = file.getFreeSpace();
		System.out.println("Free space= " + value + " bytes");
	}
}

You can find the number of unallocated bytes of a disc or partition using the method getFreeSpace().

Output:

Free space= 14719352832 bytes

Ads