package org.kodejava.example.io;
import java.io.File;
public class FreeSpaceExample {
public static void main(String[] args) {
// We create an instance of a File to represent a partition
// of our file system. For instance here we used a drive D:
// as in Windows operating system.
File file = new File("D:");
// Using the getTotalSpace() we can get an information of
// the actual size of the partition, and we convert it to
// mega bytes.
long totalSpace = file.getTotalSpace() / (1024 * 1024);
// Next we get the free disk space as the name of the
// method shown us, and also get the size in mega bytes.
long freeSpace = file.getFreeSpace() / (1024 * 1024);
// Just print out the values.
System.out.println("Total Space = " + totalSpace + " Mega Bytes");
System.out.println("Free Space = " + freeSpace + " Mega Bytes");
}
}
Here is the result of the program:
Total Space = 953739 Mega Bytes
Free Space = 952735 Mega Bytes
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019