Java URI getPath()方法及示例

来源:这里教程网 时间:2026-02-17 21:15:34 作者:

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/

相关推荐