目录

long getFreeSpace()

描述 (Description)

java.io.File.getFreeSpace()方法返回此抽象路径名指定的分区中未分配的字节数。 返回的未分配字节数不是保证。 在此调用之后,未分配字节的数量可能很准确,并且任何外部I/O操作都不准确。

声明 (Declaration)

以下是java.io.File.getFreeSpace()方法的声明 -

public long getFreeSpace()

参数 (Parameters)

NA

返回值 (Return Value)

此方法返回分区上未分配的字节。

异常 (Exception)

SecurityException - 如果存在安全管理器并且它拒绝RuntimePermission(“getFileSystemAttributes”)或其SecurityManager。 checkRead(String)拒绝此抽象路径名对文件名的读访问权。

例子 (Example)

以下示例显示了java.io.File.getFreeSpace()方法的用法。

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("C:\\test.txt");
         // get number of unallocated bytes
         v = f.getFreeSpace();
         // true if the file path exists
         bool = f.exists();
         // if file exists
         if(bool) {
            // prints
            System.out.print("number of unallocated bytes: "+v);
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

假设我们有一个文本文件c:/test.txt ,它具有以下内容。 该文件将用作示例程序的输入 -

ABCDEFGHIJKLMNOPQRSTUVWXYZ

让我们编译并运行上面的程序,这将产生以下结果 -

number of unallocated bytes: 87738191872
↑回到顶部↑
WIKI教程 @2018