HOME      BLOG       FORUMS      CONTACT       ABOUT

Finds out total used space in a partition using java

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);
	}
}

Share

Tags: , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply