java file pathname_Java Path getFileName()用法及代码示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值