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 denoted 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 partition 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 .
import java.io.*;
public class solution {
public static void main(String args[])
{
File f = new File( "F:\\program.txt" );
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 .
import java.io.*;
public class solution {
public static void main(String args[])
{
File f = new File( "F:\\program1.txt" );
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.
Last Updated :
20 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...