• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How To Get Free Disk Space In Java (Windows)

January 26, 2014 //  by Krishna Srinivasan//  Leave a Comment

This simple Java program helps you to get the free disk space on your windows machines. Here I have used three methods for getting the disk space from your system.

  1. getTotalSpace()
  2. getUsableSpace()
  3. getFreeSpace()

The above methods are introduced from Java 6.0 and as part of the java.io package. The below example prints the disk space in bytes and mega bytes.

DiskSpaceExample.java

package javabeat.net.core;

import java.io.File;
import java.io.IOException;

public class DiskSpaceExample {
	public static void main(String[] args) {
		File file = new File("d:");

		long usableSpaceVar = file.getUsableSpace();
		long totalSpaceVar = file.getTotalSpace();
		long freeSpaceVar = file.getFreeSpace();

		System.out.println(" ------- Disk Space Information in Bytes------");

		System.out.println("Total disk size for d:  " + totalSpaceVar + " bytes");
		System.out.println("Space free on d: " + usableSpaceVar + " bytes");
		System.out.println("Space free on d: " + freeSpaceVar + " bytes");

		System.out.println(" ------- Disk Space Information in Mega Bytes (MB)------");

		System.out.println("Total disk size for d: " + totalSpaceVar / 1024 / 1024 + " mega bytes");
		System.out.println("Space free on d: " + usableSpaceVar / 1024 / 1024 + " mega bytes");
		System.out.println("Space free on d: " + freeSpaceVar / 1024 / 1024 + " mega bytes");
	}
}

Output…

 ------- Disk Space Information in Bytes------
Total disk size for d:  4853467889 bytes
Space free on d: 5853467889 bytes
Space free on d: 5853467889 bytes
 ------- Disk Space Information in Mega Bytes (MB)------
Total disk size for d: 4628 mb
Space free on d: 5582 mb
Space free on d: 5582 mb

Category: JavaTag: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « How To Test Internet Connection Using Java
Next Post: How To Get IP Address In Java »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact