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


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


如果此文件存儲區是隻讀的,則FileStore類的isReadOnly()方法用於返回true。如果文件存儲不支持文件的寫操作或其他更改,則稱為隻讀。如果嘗試創建文件,打開現有文件進行寫入等,則將引發IOException。

用法:

public abstract String isReadOnly()

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


返回值:僅當此文件存儲為隻讀時,此方法返回true。

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

// Java program to demonstrate 
// FileStore.isReadOnly() 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) 
        throws IOException 
    { 
  
        // create object of Path 
        Path path 
            = Paths.get( 
                "E:\\Tutorials\\file.txt"); 
  
        // get FileStore object 
  
        FileStore fs 
            = Files.getFileStore(path); 
  
        // print FileStore name 
        System.out.println("FileStore Name: "
                           + fs.name()); 
        // is file is readable 
        boolean isReadOnly = fs.isReadOnly(); 
        System.out.println("FileStore isReadOnly:"
                           + isReadOnly); 
    } 
}

輸出:

示例2:

// Java program to demonstrate 
// FileStore.isReadOnly() 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) 
        throws IOException 
    { 
  
        // create object of Path 
        Path path 
            = Paths.get( 
                "C:\\Movies\\001.txt"); 
  
        // get FileStore object 
  
        FileStore fs 
            = Files.getFileStore(path); 
  
        // print FileStore name 
        System.out.println("FileStore Name: "
                           + fs.name()); 
        // is file is readable 
        boolean isReadOnly = fs.isReadOnly(); 
        System.out.println("FileStore isReadOnly:"
                           + isReadOnly); 
    } 
}

輸出:

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



相關用法


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