Java.io.File.getTotalSpace() Method
Advertisements
Description
The java.io.File.getTotalSpace() method returns the size of the partition named by this abstract pathname.
Declaration
Following is the declaration for java.io.File.getTotalSpace() method:
public long getTotalSpace()
Parameters
NA
Return Value
The method returns the size, in bytes, of the partition.
Exception
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.
Example
The following example shows the usage of java.io.File.getTotalSpace() method.
package com.tutorialspoint;
import java.io.File;
public class FileDemo {
public static void main(String[] args) {
File f = null;
long v;
boolean bool = false;
try{
// create new file
f = new File("test.txt");
// returns size of the partition named
v = f.getTotalSpace();
// true if the file path exists
bool = f.exists();
// if file exists
if(bool)
{
// prints
System.out.print("size: "+v);
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result:
size: 290391584768