java uri getpath_Java URI getPath()用法及代码示例

getPath()函数是URI类的一部分。函数getPath()返回指定URI的路径名称。

函数签名:

public String getPath()

用法:

url.getPath()

参数:此功能不需要任何参数

返回类型:函数返回字符串类型

以下示例程序旨在说明getPath()函数的用法:

范例1:给定URI,我们将使用getPath()函数获取路径。

// Java program to show the

// use of the function getPath()

import java.net.*;

class Solution {

public static void main(String args[])

{

// uri  object

URI uri = null;

try {

// create a URI

uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/");

// get the  Path

String _Path = uri.getPath();

// display the URI

System.out.println("URI = " + uri);

// display the  Path

System.out.println(" Path=" + _Path);

}

// if any error occurs

catch (URISyntaxException e) {

// display the error

System.out.println("URI Exception =" + e.getMessage());

}

}

}

输出:

URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/

Path=/url-getprotocol-method-in-java-with-examples/

范例2:getPath()和getRawPath()返回的值是相同的,只是对所有转义八位位组的序列进行了解码。 getRawPath()返回用户提供的字符串的确切值,但getPath()函数解码转义八位字节的序列(如果有)。

// Java program to show the

// use of the function getPath()

import java.net.*;

class Solution {

public static void main(String args[])

{

// uri  object

URI uri = null;

try {

// create a URI

uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples%E2%82%AC/");

// get the  Path

String _Path = uri.getPath();

// get the  Raw Path

String Raw_Path = uri.getRawPath();

// display the URI

System.out.println("URI = " + uri);

// display the  Path

System.out.println(" Path=" + _Path);

// display the  Raw_Path

System.out.println(" Raw Path=" + Raw_Path);

}

// if any error occurs

catch (URISyntaxException e) {

// display the error

System.out.println("URI Exception =" + e.getMessage());

}

}

}

输出:

URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples%E2%82%AC/

Path=/url-getprotocol-method-in-java-with-examples?/

Raw Path=/url-getprotocol-method-in-java-with-examples%E2%82%AC/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值