Path接口已添加到Java 7中的Java NIO中。Path接口位于java.nio.file包中,因此Java Path接口的标准名称是java.nio.file.Path。 Java Path实例表示文件系统中的路径。路径可以用来定位文件或目录。实体的路径可以是两种,一种是绝对路径,另一种是相对路径。绝对路径是从根到实体的位置地址,而相对路径是相对于其他路径的位置地址。
java.nio.file.Path的getFileName()方法用于返回此路径对象指向的文件或目录的名称。文件名是目录层次结构中距离根目录最远的元素。
用法:
Path getFileName()
参数:此方法不接受任何内容。
返回值:此方法返回此路径对象指向的文件或目录的名称。
以下示例程序旨在说明getFileName()方法:
示例1:
// Java program to demonstrate
// java.nio.file.Path.getFileName() method
import java.io.IOException;
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 getFileName() and get FileName path object
Path fileName = path.getFileName();
// print FileName
System.out.println("FileName: "
+ fileName.toString());
}
}
输出:
FileName: AmanCV.docx
示例2:
// Java program to demonstrate
// java.nio.file.Path.getFileName() method
import java.io.IOException;
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 getFileName() and get FileName path object
Path fileName = path.getFileName();
// print FileName
System.out.println("FileName: "
+ fileName.toString());
}
}
输出:
FileName: Resume.pdf