MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

How to Check Free Disk Space in Java

January 9, 2025 By Prasanna Manjunatha

For those who manage file systems and storage in Java applications, monitoring disk space is crucial. Java provides straightforward ways to check free disk space, ensuring that applications handle storage efficiently and avoid issues related to insufficient space. This article will walk you through how to check free disk space in Java, complete with the necessary code snippets.

  • JaCoCo Code Coverage in Springboot Applications : A Comprehensive Guide
  • How to Password Protect a PDF File using Java : A Step-by-Step Guide\

How to Check Free Disk Space in Java

import java.io.File;

public class DiskSpaceChecker {
    public static void main(String[] args) {
        // Check primary disk
        File diskPartition = new File("/");
        long freeSpace = diskPartition.getFreeSpace();
        double freeSpaceGB = freeSpace / (1024.0 * 1024.0 * 1024.0);
        System.out.println("Free disk space (GB): " + freeSpaceGB);

        // Check total and usable space
        long totalSpace = diskPartition.getTotalSpace();
        long usableSpace = diskPartition.getUsableSpace();

        // Check multiple partitions
        File[] roots = File.listRoots();
        for (File root : roots) {
            System.out.println("Root: " + root.getAbsolutePath());
            System.out.println("Free space (GB): " + root.getFreeSpace() / (1024.0 * 1024.0 * 1024.0));
        }
    }
}

 

  •  A File object is created to represent the disk partition. For primary disk checks, we use new File(“/”).
  • The getFreeSpace() method fetches free space in bytes. It’s converted to Gigabytes (GB) for readability.
  • getTotalSpace() gives the total space of the partition, while getUsableSpace() provides the space available to the JVM.
  • By using File.listRoots(), we can iterate over different roots (or partitions) in the file system, checking each partition’s free space.

 

Also Read: How to iterate Java HashMap

Conclusion

Checking disk space in Java is a breeze thanks to the straightforward methods provided by the java.io.File class. Whether you’re dealing with a single partition or multiple ones, these methods can be seamlessly integrated into your Java applications for effective storage management and monitoring.

email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 CodeCoverage Code Coverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Spring boot Springboot SQL Tools Tutorial web service

All time Popular Posts

Copyright © 2025 MyJavaCode.com