JavaBeat

  • Home
  • 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)
  • Privacy

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

[code lang=”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");
}
}
[/code]

Output…

[code]
——- 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
[/code]

Filed Under: Java Tagged With: 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.

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.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved