java sub_Java Path subpath()用法及代码示例

java.nio.file.Path的subpath(int beginIndex,int endIndex)方法用于返回相对路径,该相对路径是此路径的名称元素的子序列。我们将通过开始索引和结束索引来构建子路径。 beginIndex和endIndex参数指定名称元素的子序列。在目录层次结构中最接近根的name元素是索引0,而离根最远的name元素的索引是count-1。返回的子路径对象具有以beginIndex开头并扩展到索引endIndex-1的元素。

用法:

Path subpath(int beginIndex,

int endIndex)

参数:此方法接受两个参数:

beginIndex这是第一个元素的索引,包括和

endIndex这是最后一个元素的索引,不包括索引。

返回值:此方法返回一个新的Path对象,该对象是该Path中名称元素的子序列。

异常:如果beginIndex为负或大于或等于元素数,则此方法引发IllegalArgumentException。如果endIndex小于或等于beginIndex,或者大于元素数。

以下示例程序旨在说明subpath()方法:

示例1:

// Java program to demonstrate

// java.nio.file.Path.subpath() method

import java.nio.file.Path;

import java.nio.file.Paths;

public class GFG {

public static void main(String[] args)

{

// create an object of Path

Path path

= Paths.get("D:\\eclipse\\p2"

+ "\\org\\eclipse\\equinox\\p2\\core"

+ "\\cache\\binary");

// call subPath() to create a subPath which

// begin at index 1 and ends at index 5

Path subPath = path.subpath(1, 5);

// print result

System.out.println("Subpath: "

+ subPath);

}

}

输出:

c3c99e9b8f3743fe24a8f0a823da3bb8.png

示例2:

// Java program to demonstrate

// java.nio.file.Path.subpath() method

import java.nio.file.Path;

import java.nio.file.Paths;

public class GFG {

public static void main(String[] args)

{

// create an object of Path

Path path

= Paths.get("D:\\Workspace"

+ "\\nEclipseWork"

+ "\\GFG\\bin\\defaultpackage");

System.out.println("Original Path:"

+ path);

// call subPath() to create a subPath which

// begin at index 0 and ends at index 2

Path subPath = path.subpath(0, 2);

// print result

System.out.println("Subpath: "

+ subPath);

}

}

输出:

f3d94e732c57643df5e9c2d7683f13aa.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值