Get Total or Free Disk Space In JavaIO

Get Total or Free Disk Space In JavaIO

 

Problem Statement: How to Get Total or Free Disk Space In JavaIO?

Description:

We can get the total or free disk space using  java.io.File.getTotalSpace(),  java.io.File.getUsableSpace(),  java.io.File.getFreeSpace()

The java.io.File.getTotalSpace() returns the number of free bytes available (un-allocated) on this virtual machine on the partition.

The java.io.File.getUsableSpace() returns the number of free bytes available (unallocated) on this virtual machine on the partition.

The java.io.File.getFreeSpace() returns the number of unallocated bytes in the partition named by this abstract path name.

Get Total or Free Disk Space In JavaIO

Get Total or Free Disk Space In JavaIO

Code

package com.stixs.java.io;
 
import java.io.File;
 
public class STXGetDriveStorageDetail
{
    public static void main(String[] args)
    { 
          File file = new File("C:");
          //The getTotalSpace() method returns size in bytes of the partition(D:) named by this abstract pathname.
          long totalAvailableSpace = file.getTotalSpace(); 
 
           //getUsableSpace() Returns the number of free bytes available (unallocated) on this virtual machine on the partition. 
           long totalUsableSpace = file.getUsableSpace(); 
 
           //getFreeSpace()Returns the number of unallocated bytes in the partition named by this abstract path name. 
           long totalFreeSpace = file.getFreeSpace(); 
 
           System.out.println("STXGetDriveStorageDetail: STX Drive Storage Detail");
 
           System.out.println("STXGetDriveStorageDetail -- In bytes.");
           System.out.println("STXGetDriveStorageDetail::Total Available Space : " + totalAvailableSpace + " in bytes");
           System.out.println("STXGetDriveStorageDetail::Total Usable Space : " + totalUsableSpace + " in bytes");
           System.out.println("STXGetDriveStorageDetail::Total Free Space : " + totalFreeSpace + " in bytes");
 
           System.out.println(" === mega bytes ===");
           long totalAvailSpaceInMegaBytes = totalAvailableSpace /1024 /1024;
           System.out.println("STXGetDriveStorageDetail::Total Available Space : " + totalAvailSpaceInMegaBytes + " in Mega Bytes.");
           long totalUsableSpaceInMegaBytes = totalUsableSpace /1024 /1024;
           System.out.println("STXGetDriveStorageDetail::Total Usable Space : " + totalUsableSpaceInMegaBytes + " in Mega Bytes.");
           long totalFreeSpaceInMegaBytes = totalFreeSpace /1024 /1024;
           System.out.println("STXGetDriveStorageDetail::Total Free Space : " + totalFreeSpaceInMegaBytes + " in Mega Bytes.");
     }
}

Output:

STXGetDriveStorageDetail: STX Drive Storage Detail
STXGetDriveStorageDetail -- In bytes.
STXGetDriveStorageDetail::Total Available Space : 238998777856 in bytes
STXGetDriveStorageDetail::Total Usable Space : 124120719360 in bytes
STXGetDriveStorageDetail::Total Free Space : 124120719360 in bytes
 === mega bytes ===
STXGetDriveStorageDetail::Total Available Space : 227926 in Mega Bytes.
STXGetDriveStorageDetail::Total Usable Space : 118370 in Mega Bytes.
STXGetDriveStorageDetail::Total Free Space : 118370 in Mega Bytes.


Java API Classes and Methods used to construct file path

getTotalSpace()

getUsableSpace

 

getFreeSpace

 

Reference

Refer below path to get the complete details about Java IO File class.

http://docs.oracle.com/javase/8/docs/api/java/io/File.html

 

——————————————————————————–

You may also like this

——————————————————————————–

About TechnoStixs.com

Get Total or Free Disk Space In JavaIO

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

999 total views, 3 views today

Add comment