java.io.File getUsableSpace()

Description

On this document we will be showing a java example on how to use the getUsableSpace() method of File Class. This method returns the number of bytes available to this virtual machine on the partition named by this abstract pathname. When possible, this method checks for write permissions and other operating system restrictions and will therefore usually provide a more accurate estimate of how much new data can actually be written than getFreeSpace().

The returned number of available bytes is a hint, but not a guarantee, that it is possible to use most or any of these bytes. The number of unallocated bytes is most likely to be accurate immediately after this call. It is likely to be made inaccurate by any external I/O operations including those made on the system outside of this virtual machine. This method makes no guarantee that write operations to this file system will succeed.

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.

Method Syntax

public long getUsableSpace()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

This method returns the number of available bytes on the partition or 0L if the abstract pathname does not name a partition. On systems where this information is not available, this method will be equivalent to a call to getFreeSpace().

Compatibility

Requires Java 1.6 and up

Java File getUsableSpace() Example

Below is a java code demonstrates the use of getUsableSpace() method of File class. The example presented might be simple however it shows the behaviour of the getUsableSpace() method of File class. To make the print outs of the usable space available we made some calculations to print out the equivalent in Kb, Mb and Gb.

Sample Output

Below is the sample output when you run the above example.

java lang File getUsableSpace() example output

Suggested Reading List

References