Quantcast
Channel: Kushal's Java Blog | Software Engineering Blog
Viewing all articles
Browse latest Browse all 25

Computing the total, free and usable disk space easily using JDK 1.6

$
0
0

Note: This tool uses Jdk1.6. It does not work for lower versions of java sdk/jdk. You can run this tool to find the disk space, free space and usable disk space although for several of my test, usable space and free space returned the same value. This capability has been added to jdk1.6 version. Hence it will not work for any lower versions.

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * This tool use Jdk1.6. Does not work for lower versions.
 * 
 * You can run this tool to find the disk space, free space and usable disk space
 * although for several of my test, usable space and free space returned the same
 * value. This capability has been added to jdk1.6 version. Hence it will not work
 * for lower versions.
 */
import java.io.File;

public class DiskSpaceJavaV6 {

	public static void main(String[] args) {
		File file = new File("c:");
		String unit="GB";
		DiskSpaceJavaV6 dspace=new DiskSpaceJavaV6();
		double totalDiskSpace = dspace.getTotalDiskSpace(file,unit );
		double usableSpace = dspace.getUsableSpace(file, unit); 
		double freeSpace = dspace.getFreeSpace(file, unit);
		
		System.out.println("Total Disk Space: " +totalDiskSpace +" "+unit);
		System.out.println("Total Usable Space : " + usableSpace +" "+unit);
		System.out.println("Free Disk Space : " + freeSpace +" "+unit);
	}
	/**
	 * @param file - normally the top level drive e.g. c:
	 * @param unit - target unit for disk space. Allowed values KB, MB, GB.
	 * @return total disk space
	 */
	public double getTotalDiskSpace(File file, String unit) {
		return processUnit(file.getTotalSpace(), unit);
	}
	/**
	 * @param file - normally the top level drive e.g. c:
	 * @param unit - target unit for disk space. Allowed values KB, MB, GB.
	 * @return usable disk space
	 */
	public double getUsableSpace(File file, String unit) {
		return processUnit(file.getUsableSpace(), unit);
	}
	
	/**
	 * @param file - normally the top level drive e.g. c:
	 * @param unit - target unit for disk space. Allowed values KB, MB, GB.
	 * @return free space
	 */
	public double getFreeSpace(File file, String unit) {
		return processUnit(file.getFreeSpace(), unit);
	}

	/**
	 * @param space - disk space in bytes
	 * @param unit - the target unit. Allowed values: KB, MB, GB.
	 * @return processed value
	 */
	private double processUnit(long space, String unit) {
		String nonNullUnit=makeNonNullUnit(unit);
		if("KB".equalsIgnoreCase(nonNullUnit)) {
				return space/1024;
		} else if("MB".equalsIgnoreCase(nonNullUnit)) {
				return space/(1024*1024);
		} else if("GB".equalsIgnoreCase(nonNullUnit)) {
			return space/(1024*1024*1024);
		} else {
			return space;
		}
	}
	/**
	 * @param anyString
	 * @return non null value of the string
	 */
	private static String makeNonNullUnit(String anyString) {
		if(anyString==null) {
			return "";
		} else {
			return anyString;
		}
	}
}

Share


Viewing all articles
Browse latest Browse all 25

Trending Articles