java pathname_Java Path getName(int)用法及代码示例

Java Path接口已添加到Java 7中的Java NIO中。Path接口位于java.nio.file包中,因此Java Path接口的标准名称是java.nio.file.Path。 Java Path实例表示文件系统中的路径。路径可以用来定位文件或目录。实体的路径可以是两种,一种是绝对路径,另一种是相对路径。绝对路径是从根到实体的位置地址,而相对路径是相对于其他路径的位置地址。

java.nio.file.Path的getName(int index)方法,用于将此路径的名称元素作为Path对象返回。我们将index作为参数传递,index表示要返回的name元素的索引。在目录层次结构中最接近根的元素的索引为0。离根最远的元素的索引为count-1。

用法:

Path getName(int index)

参数:此方法接受单个参数索引,该索引是元素的索引。

返回值:此方法返回name元素。

异常:如果索引为负,索引大于或等于元素数,或者此路径的名称元素为零,则抛出此方法和Exception IllegalArgumentException异常。

以下示例程序旨在说明getName(int index)方法:

示例1:

// Java program to demonstrate

// java.nio.file.Path.getName(int index) 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:\\temp\\DS and algo"

+ "\\algorithim and data struc"

+ "\\Problem sets");

// call getName(int i) to get the

// element at index i

Path indexpath = path.getName(3);

// print ParentPath

System.out.println("Index 3: "

+ indexpath);

}

}

输出:

Index 3: Problem sets

示例2:

// Java program to demonstrate

// java.nio.file.Path.getName(int index) 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:\\eclipse\\configuration"

+ "\\org.eclipse.update");

// call getName(int i) to get

// the element at index i

Path indexpath = path.getName(2);

// print ParentPath

System.out.println("Index 2: "

+ indexpath);

}

}

输出:

Index 2: org.eclipse.update

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值