Calculate Free Disk Space in Java using Apache Commons IO

As a Java developer, lot of times I have to play around with file system. Sometimes I have to copy files/directories from one location to another; sometimes have to process certain files depending on certain pattern. In one of my test program, I wanted to calculate available disk space using Java. Lot of code snippets are available for this task. I liked the one using Apache Commons IO library. Here is a simple trick for Java developers to calculate free diskspace. We have used Apache Commons IO library to calculate this. Apache Commons IO library contains a class org.apache.commons.io.FileSystemUtils which can be used to calculate the free disk space in any system. Let us see the Java code for this.
package net.viralpatel.java; import java.io.IOException; import org.apache.commons.io.FileSystemUtils; public class DiskSpace { public static void main(String[] args) { try { //calculate free disk space double freeDiskSpace = FileSystemUtils.freeSpaceKb("C:"); //convert the number into gigabyte double freeDiskSpaceGB = freeDiskSpace / 1024 / 1024; System.out.println("Free Disk Space (GB):" + freeDiskSpaceGB); } catch (IOException e) { e.printStackTrace(); } } }
Code language: Java (java)
Output:
Free Disk Space (GB): 40.145268
Code language: CSS (css)
In above code we used FileSystemUtils.freeSpaceKb( ) method to get the free space in kilo byte. This method invokes the command line to calculate the free disk space. You may want to call this method in following way to get free disk space in Windows and Linux.
FileSystemUtils.freeSpaceKb("C:"); // Windows FileSystemUtils.freeSpaceKb("/volume"); // *nix
Code language: Java (java)
The free space is calculated via the command line. It uses ‘dir /-c’ on Windows, ‘df -kP’ on AIX/HP-UX and ‘df -k’ on other Unix. In order to work, you must be running Windows, or have a implementation of Unix df that supports GNU format when passed -k (or -kP). If you are going to rely on this code, please check that it works on your OS by running some simple tests to compare the command line with the output from this class.

View Comments

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

5 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

5 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

5 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

5 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago