Back to Posts

Build a Java Disk Space Calculator Command Line Application

Build a Java Disk Space Calculator Command Line Application

Home > Java > Desktop Applications > Build a Java Disk Space Calculator Command Line Application

Build a Java Disk Space Calculator Command Line Application

One of the best ways to learn Java is by building small utilities that solve real problems. In this tutorial, we'll create a command line application that displays the amount of free and total disk space available on your computer.

This project introduces the File class and shows how Java can retrieve information about the file system without requiring any third-party libraries.

What Will the Program Do?

Our application will:

  • Display the drive letter.
  • Show the total disk size.
  • Display the amount of free space.
  • Display the amount of used space.
  • Display all values in gigabytes.

Create the Project

Create a new Java project with a single class called Main.


src
└── Main.java

The Complete Program

Copy the following code into Main.java.


import java.io.File;

public class Main {

    public static void main(String[] args) {

        File[] drives = File.listRoots();

        for (File drive : drives) {

            long total = drive.getTotalSpace();
            long free = drive.getFreeSpace();
            long used = total - free;

            long gb = 1024L * 1024L * 1024L;

            System.out.println("--------------------------------");
            System.out.println("Drive : " + drive);
            System.out.println("Total : " + (total / gb) + " GB");
            System.out.println("Used  : " + (used / gb) + " GB");
            System.out.println("Free  : " + (free / gb) + " GB");
        }

    }

}

Compile the Program

Open a command prompt and compile the application.


javac Main.java

Now run it.


java Main

Example Output


--------------------------------
Drive : C:\
Total : 476 GB
Used  : 221 GB
Free  : 255 GB

--------------------------------
Drive : D:\
Total : 931 GB
Used  : 418 GB
Free  : 513 GB

Understanding the Code

The File.listRoots() method returns every drive that Java can see on the computer.

For each drive we retrieve three important values.

  • getTotalSpace() returns the total capacity of the drive.
  • getFreeSpace() returns the unused space.
  • Used Space is calculated by subtracting the free space from the total space.

Java returns these values in bytes, so we divide them by 1,024 × 1,024 × 1,024 to display them as gigabytes.

Ideas for Improvement

Once you have this working, try extending the application.

  • Display disk usage as a percentage.
  • Round values to two decimal places.
  • Accept a drive letter as a command line argument.
  • Display the results in a formatted table.
  • Export the information to a text file.

What You Have Learned

Although this is a small utility, it introduces an important part of the Java Standard Library. The File class provides information about files, folders and disk drives, making it useful for backup utilities, system monitoring tools and desktop applications.

Projects like this are far more representative of real Java development than inheritance examples involving animals or vehicles. They teach you how to solve practical problems while becoming familiar with the classes provided by the Java Standard Library.

Back to Posts