File getUsableSpace() method in java with examples
The getUsableSpace() function is a part of File class in Java . This function returns the unallocated size of the partition denoted by the abstract pathname, if the pathname does not exist then it returns 0L. This function is more accurate than the getFreeSpace() function as it returns the free space allocated to this virtual machine. This function gives a hint of the unallocated space of the partition but does not give any guarantee that the exact number of bytes is Usable. An error might be caused if an external application writes to the specified file, then the write operation might not succeed. If there is no clear distinction it will return the same value as getFreeSapce() method.
Function signature:
public long getUsableSpace()
Syntax:
long var = file.getUsableSpace();
Parameters: This method does not accept any parameter.
Return Value: The function returns long data type represents unallocated size of the partition in bytes
Exception: This method throws SecurityException if the method does not allow file to be created
Below programs illustrates the use of getUsableSpace() function:
Example 1: The file “F:\\program.txt” is a existing file in F: Directory .
// Java program to demonstrate // getUsableSpace() method of File Class import java.io.*; public class solution { public static void main(String args[]) { // Create an abstract pathname (File object) File f = new File("F:\\program.txt"); // Display the Usable size of the partition // using the getUsableSpace() function System.out.println("Usable Space: " + f.getUsableSpace()); } } |
Output:
Usable Space: 174491860992
Example 2: The file “F:\\program1.txt” does not exist file in F: Directory .
// Java program to demonstrate // getUsableSpace() method of File Class import java.io.*; public class solution { public static void main(String args[]) { // Create an abstract pathname (File object) File f = new File("F:\\program1.txt"); // Display the Usable size of the partition // using the getUsableSpace() function System.out.println("Usable Space: " + f.getUsableSpace()); } } |
Output:
Usable Space: 0
Note: The programs might not run in an online IDE. Please use an offline IDE and set the path of the file.
Recommended Posts:
- FileStore getUsableSpace() method in Java with Examples
- File listRoots() method in Java with examples
- File getCanonicalPath() method in Java with Examples
- File getAbsoluteFile() method in Java with Examples
- File mkdirs() method in Java with examples
- File listFiles() method in Java with Examples
- File isFile() method in Java with Examples
- File mkdir() method in Java with examples
- File getAbsolutePath() method in Java with Examples
- File setExecutable() method in Java with Examples
- File renameTo() method in Java with examples
- File getFreeSpace() method in Java with examples
- File createTempFile() method in Java with Examples
- File delete() method in Java with Examples
- File canExecute() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh


