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

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

  1. How to extract plain Text from HTML Website easily in Java
  2. How to read / write UTF8 and Non-UTF8 files in Java
  3. How To Print A Text File In Java
  4. How to read a tab separated or tab delimited file in Java program and print the content to console
  5. How To Find What Java Version You Are Using?
  6. Computing the total, free and usable disk space easily using JDK 1.6
  7. Reading the Resource or Property files in Java as a file stream and outputting the content to console
  8. Creating java.util.date and java.util.sql objects from Calendar object
  9. File Copy From Local Folder or Network Folder Using Java With Capability To Limit The Number
  10. Bar Code Generation In Java Using iText API
  11. Simple Tag Generator In Java For Music Blogs
  12. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  13. Calling URL Browser From Java Application
  14. Java Reflection Tutorial – List Methods Of A Class
  15. Cleaning Punctuation And White Spaces From A String
  16. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  17. Prime Number Finder In Java
  18. Utility Class For Getting SQL Connection and Closing It
  19. Java Design Pattern – Singleton Pattern
  20. Reading / Writing File in Java and String Manipulation
  21. How to capture a screenshot using Java and save it to a file?
  22. Calculating The Difference Between Two Dates In Terms of Months
  23. Getting The Name Of Currently Logged In User In The System
  24. How To Set And Get System Clipboard Contents In Java
  25. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  26. Generating Nepali Unicode Sequence For Characters
  27. How To Read DOC file Using Java and Apache POI
  28. Writing To Excel File Using Apache POI
  29. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  30. Replacing non-ASCII characters using Java Regular Expressions
  31. Reading Excel File Using Java And Apache POI
  32. Calculating Folder Size In Java
  33. How To Rename A File In Java
  34. Calculating Folder Size Graphically and Generating Directory Size Chart In Java
Tagged , , , , , , , . Bookmark the permalink.

One Response to Calculating Folder Size Graphically and Generating Directory Size Chart In Java

  1. nocdib says:

    Thank you for this! It is just what I need

Leave a Reply

Your email address will not be published. Required fields are marked *