File getFreeSpace() in Java

getFreeSpace(): This method is available in the java.io.File class of Java.

Syntax:

long java.io.File.getFreeSpace()

This method returns the number of unallocated bytes in the partition named by this abstract path name. If the number of unallocated bytes in the partition is greater than Long.MAX_VALUE, then Long.MAX_VALUE will be returned.

Parameters: NA

Returns: The number of unallocated bytes on the partition or 0L if the abstract pathname does not name a partition or if this number cannot be obtained.

Throws:

SecurityException - If a security manager has been installed and it denies RuntimePermission("getFileSystemAttributes") or its SecurityManager.checkRead(String) method denies read access to the file named by this abstract pathname.

Approach

Java

import java.io.File;

public class FilegetFreeSpace {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.getFreeSpace());
    }
}

Output:

144047357952


No comments:

Post a Comment