📜  Java中的 FileStore isReadOnly() 方法及示例

📅  最后修改于: 2022-05-13 01:55:45.045000             🧑  作者: Mango

Java中的 FileStore isReadOnly() 方法及示例

如果此文件存储为只读,则使用FileStore类的isReadOnly()方法返回 true,否则返回 false。如果文件存储不支持写入操作或对文件的其他更改,则称为只读文件存储。如果尝试创建文件、打开现有文件进行写入等,则会引发 IOException。

句法:

public abstract String isReadOnly()

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

返回值:当且仅当此文件存储为只读时,此方法才返回true

下面的程序说明了 isReadOnly() 方法:
方案一:

// 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);
    }
}

输出:

方案二:

// 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: Java/nio/file/FileStore.html#isReadOnly()