当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java FileStore getUnallocatedSpace()用法及代码示例


FileStore类的getUnallocatedSpace()方法用于获取文件存储中未分配的字节数。此方法可用于检查可用空间。此方法将可用的未分配空间作为long值返回。

用法:

public abstract long getUnallocatedSpace()
                     throws IOException

参数:此方法不接受任何内容。


返回值:此方法将可用的未分配空间作为long值返回。

异常:如果发生I /O错误,则此方法将引发IOException。

以下示例程序旨在说明getUnallocatedSpace()方法:
示例1:

// Java program to demonstrate 
// FileStore.getUnallocatedSpace() method 
  
import java.io.IOException; 
import java.nio.file.FileStore; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // create the object of Path 
        Path path 
            = Paths.get( 
                "E:\\Tutorials\\file.txt"); 
  
        // get FileStore object 
        try { 
  
            FileStore fs 
                = Files.getFileStore(path); 
  
            // print FileStore name and Total unused size 
            System.out.println("FileStore Name: "
                               + fs.name()); 
            long bytes = fs.getUnallocatedSpace(); 
            long sizeInGB = bytes / (1024 * 1024 * 1024); 
            System.out.println("Free Space : "
                               + sizeInGB + "GB"); 
        } 
        catch (IOException e) { 
  
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}

输出:

示例2:

// Java program to demonstrate 
// FileStore.getUnallocatedSpace() method 
  
import java.io.IOException; 
import java.nio.file.FileStore; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // create the object of Path 
        Path path 
            = Paths.get( 
                "C:\\Movies\\001.txt"); 
  
        // get FileStore object 
        try { 
  
            FileStore fs 
                = Files.getFileStore(path); 
  
            // print FileStore name and Total size 
            System.out.println("FileStore Name: "
                               + fs.name()); 
            System.out.println("Free Space in bytes: "
                               + fs.getUnallocatedSpace()); 
        } 
        catch (IOException e) { 
  
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}

输出:

参考:https://docs.oracle.com/javase/10/docs/api/java/nio/file/FileStore.html#getUnallocatedSpace()



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 FileStore getUnallocatedSpace() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。