long getTotalSpace()
描述 (Description)
java.io.File.getTotalSpace()方法返回此抽象路径名指定的分区的大小。
声明 (Declaration)
以下是java.io.File.getTotalSpace()方法的声明 -
public long getTotalSpace()
参数 (Parameters)
NA
返回值 (Return Value)
该方法返回分区的大小(以字节为单位)。
异常 (Exception)
SecurityException - 如果已安装安全管理器并且它拒绝RuntimePermission(“getFileSystemAttributes”)或其SecurityManager.checkRead(String)方法拒绝对该文件的读访问。
例子 (Example)
以下示例显示了java.io.File.getTotalSpace()方法的用法。
package com.iowiki;
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();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
size: 290391584768