如果此文件存储区是只读的,则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()