Java source code examples

Java source code samples. Java code examples.

Finds out total used space in a partition using java

* * * * * 1 votes

Finds out total used space in a partition using java

In Java we can find the total used space of any drive or partition using getTotalSpace and getFreeSpace methods.

import java.io.File;
/**
 * File class examples.
 *
 */
public class FileExample  {
	public static void main(String[] args){
		// Create file object representing the source file/directory
		File file = new File("d:\\temp\\test.txt");

		// Get the total space available in current drive
		long totalSpace = file.getTotalSpace();

		// Get the total free space available in current partition
		long freeSpace = file.getFreeSpace();

		// Finds the total used space
		long value = totalSpace - freeSpace;

		// Convert it to GigaBytes
		double valueGB = (double) value / 1024 / 1024 / 1024;

		System.out.println("Total used space in the current partition is "+valueGB);
	}
}

Tags: java.io

Discuss This Code