當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java FileStore getTotalSpace()用法及代碼示例


FileStore類的getTotalSpace()方法用於返回文件存儲的總大小(以字節為單位)。此方法以long值形式返回總大小。

用法:

public abstract long getTotalSpace()
                throws IOException

參數:此方法不接受任何內容。


返回值:此方法以long值形式返回文件存儲的總大小。

異常:如果發生I /O錯誤,則此方法將引發IOException。

以下示例程序旨在說明getTotalSpace()方法:

示例1:

// Java program to demonstrate 
// FileStore.getTotalSpace() 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 size 
            System.out.println("FileStore Name: "
                               + fs.name()); 
            System.out.println("FileStore TotalSpace: "
                               + fs.getTotalSpace()); 
        } 
        catch (IOException e) { 
  
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}

輸出:


示例2:

// Java program to demonstrate 
// FileStore.getTotalSpace() 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("FileStore TotalSpace: "
                               + fs.getTotalSpace()); 
        } 
        catch (IOException e) { 
  
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}

輸出:


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



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 FileStore getTotalSpace() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。