Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java File – display space on disk

farenda 2016-02-01 0

Problem:

How to get and display disk space information in Java? In this post we’re going to show an example that does that using Java File from java.io package.

Since Java 6 the java.io.File class comes with three methods that provide information about disk space on partition specified by file:

  • public long getTotalSpace()
    Returns total number of bytes on the current partition.
  • public long getFreeSpace()
    Returns number of unallocated bytes on the current partition.
  • public long getUsableSpace()
    Returns number of bytes on the current partition available for this JVM.

To use the methods you need to create an instance of File object that will have some path name. This way JVM can get data for the partition on which the file is located. Let’s see that in action!

Solution:

The code is very simple. We create a File object that point to the current directory, which means that we want space information the partition on which the current directory is located. Next we get the data and convert to human-friendly form – gigabytes (keep in mind that the numbers returned by the space methods are in bytes):

package com.farenda.java.io;

import java.io.File;

public class FileDiskUsageExample {

    private static final int GB = 1024 * 1024 * 1024;

    public static final void main(final String[] args) {
        File file = new File(".");
        System.out.printf("    Total: %d GB%n", file.getTotalSpace() / GB);
        System.out.printf("     Free: %d GB%n", file.getFreeSpace() / GB);
        System.out.printf("Available: %d GB%n", file.getUsableSpace() / GB);
    }

}

Sample output of the program on my laptop:

    Total: 269 GB
     Free: 183 GB
Available: 169 GB

Note that not all the size reported as Free is Available for use by this Java Virtual Machine. That’s why the numbers are different.

Share with the World!
Categories Java Tags java, java-io
Previous: JUnit Theories with homemade ParamaterSuppliers
Next: Java File list files

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored