Home Tutorial Java Corejava Display free disk space

 
 

Display free disk space
Posted on: April 20, 2010 at 12:00 AM
This tutorial demonstrate how to display total free disk space of each drive.

Description:

This example will demonstrate how to get the free disk space for your drive. The getFreeSpace method introduced in the Jdk 6 and found in java.io.File. This method get the free disk space in the bytes.

Code:

import java.io.File;

public class FreeSpace {
  public static void main(String[] args) {
  File[] drive = File.listRoots();

    for (int i = 0; i < drive.length; i++) {
      System.out.println(drive[i]);
    double freebyte = drive[i].getFreeSpace();
    double temp = Math.pow(1024,3);
    double freegb = (double)freebyte/temp;
    System.out.println("Free space "+freegb+" GB");
     }
  }
}

Output:

The output of this example depends upon number of drive you have and the free space available.

Note : As it search for drive and free space it will take time to display the result so wait till it complete searching.

Sample output:

Display total disk drive capacity
This tutorial demonstrate how to display total disk capacity of each drive.
 
Path of source code
This tutorial demonstrate how to get the path of your source code
 
How to make a file read-only
This tutorial demonstrate how to create file and set its attribute read-only
 

Related Tags for Display free disk space:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.