Java Path getFileSystem()方法及示例
在 Java 7 中,Path接口被添加到Java NIO中 。 Path 接口位于 java.nio.file 包中,所以 Java Path 接口的全称是 java.nio.file.Path。一个Java Path实例代表文件系统中的一个路径。路径可以用来定位文件或目录。实体的路径有两种类型,一种是绝对路径,另一种是相对路径。绝对路径是指从根到实体的位置地址,而相对路径是指相对于其他路径的位置地址。
java.nio.file.Path 的 getFileSystem() 方法用于返回创建此Path对象的文件系统。
语法
FileSystem getFileSystem()
参数: 此方法不接受任何东西。
返回值: 该方法返回创建该路径对象的文件系统。
以下程序说明了getFileSystem()方法:
程序1 :
// Java program to demonstrate// java.nio.file.Path.getFileSystem() method import java.io.IOException;import java.nio.file.FileSystem;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("D:/workspace/AmanCV.docx"); // call getFileSystem() // and get FileSystem object FileSystem fs = path.getFileSystem(); // print separator of FileSystem System.out.println("Separator used for FileSystem: " + fs.getSeparator()); }}输出。
Separator used for FileSystem: /
程序2
// Java program to demonstrate// java.nio.file.Path.getFileSystem() method import java.io.IOException;import java.nio.file.FileSystem;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("D:/Resume.pdf"); // call getFileSystem() // and get FileSystem object FileSystem fs = path.getFileSystem(); // print FileSystem is ReadOnly or not System.out.println("FileSystem is ReadOnly: " + fs.isReadOnly()); }}输出。
FileSystem is ReadOnly: false
参考文献: https://docs.oracle.com/javase/10/docs/api/java/nio/file/Path.html#getFileSystem()
