Home  >  Core Java

getTotalSpace,getFreeSpace and getUsableSpace of File in Java

By Arvind Rai, February 06, 2013
In JDK 1.6, File class has some new methods. File provides getTotalSpace method to get total space in the specified directory by File class. getFreeSpace provides the available free space. getUsableSpace provides the space available to virtual machine. These methods returns long value and these are Byte data.
These methods are useful when we want to write some data on disk and need to know how much space are there to write data successfully. This is possible that just after getting disk space information from File class method, any other resource has written some data on disk. So these File class method are not giving guarantee to write data successfully.
package com.concretepage;
import java.io.File;

public class FileTest {

    public static void main(String... args){

        File file= new File("c:");

        //gets the total space in C drive in Byte

        long totalSpace=file.getTotalSpace();

        System.out.println("Total Space in Byte:"+totalSpace);

        //gets the free space in C drive in Byte

        long freeSpace=file.getFreeSpace();

        System.out.println("Free Space in Byte:"+freeSpace);

        //gets the usable space in c drive in Byte
        long usableSpace=file.getUsableSpace();
        System.out.println("Usable Space in Byte:"+usableSpace);
Total Space in Byte:63042482176
Free Space in Byte:6885539840
Usable Space in Byte:6885539840

©2022 concretepage.com | Privacy Policy | Contact Us