File getTotalSpace() method in Java with examples
The getTotalSpace() function is a part of File class in Java . This function returns the total size of the partition donoted by the abstract pathname, if the pathname does not exist then it returns 0L.
Function signature:
public long getTotalSpace()
Syntax:
long var = file.getTotalSpace();
Parameters: This method does not accept any parameter.
Return Value: The function returns long data type represents size of the partiton in bytes
Exception: This method throws SecurityException if the method does not allow file to be created
Below programs illustrates the use of getTotalSpace() function:
Example 1: The file “F:\\program.txt” is a existing file in F: Directory .
// Java program to demonstrate // getTotalSpace() 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 Total size of the partition // using the getTotalSpace() function System.out.println("Total Space: " + f.getTotalSpace()); } } |
Output:
Total Space: 355947507712
Example 2: The file “F:\\program1.txt” does not exist file in F: Directory .
// Java program to demonstrate // getTotalSpace() 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 Total size of the partition // using the getTotalSpace() function System.out.println("Total Space: " + f.getTotalSpace()); } } |
Output:
Total 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:
- File mkdirs() method in Java with examples
- File getParent() method in Java with Examples
- File createNewFile() method in Java with Examples
- File lastModified() method in Java with Examples
- File getUsableSpace() method in java with examples
- File setReadOnly() method in Java with examples
- File listFiles() method in Java with Examples
- File isFile() method in Java with Examples
- File canExecute() method in Java with Examples
- File mkdir() method in Java with examples
- File getAbsoluteFile() method in Java with Examples
- File getCanonicalPath() method in Java with Examples
- File canWrite() method in Java with examples
- File isDirectory() method in Java with Examples
- File setExecutable() 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.



