Java URI getPath()方法及示例

Java URI getPath()方法及示例

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

函数签名

public String getPath()

语法:

url.getPath()

参数 :该函数不需要任何参数
返回类型: 该函数返回字符串类型

下面的程序说明了getPath()函数的使用。

例1 :给定一个URL,我们将使用getPath()函数获得路径。

// Java program to show the
// use of the function getPath()
  
import java.net.*;
  
class Solution {
    public static void main(String args[])
    {
        // url  object
        URL url = null;
  
        try {
            // create a URL
            url = new URL(
                "https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/");
  
            // get the  Path
            String _Path = url.getPath();
  
            // display the URL
            System.out.println("URL = " + url);
  
            // display the  Path
            System.out.println(" Path= " + _Path);
        }
        // if any error occurs
        catch (Exception e) {
  
            // display the error
            System.out.println(e);
        }
    }
}

输出:

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

例2 :现在看看getPath()和getFile()有什么不同。getPath()将排除查询,但getFile()将包括查询。

// Java program to show the
// use of the function getPath()
  
import java.net.*;
  
class Solution {
    public static void main(String args[])
    {
        // url  object
        URL url = null;
  
        try {
            // create a URL
            url = new URL(
                "https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol");
  
            // get the  Path
            String _Path = url.getPath();
  
            // display the URL
            System.out.println("URL = " + url);
  
            // display the  Path
            System.out.println(" Path= " + _Path);
        }
        // if any error occurs
        catch (Exception e) {
  
            // display the error
            System.out.println(e);
        }
    }
}

输出:

URL = https:// www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol
 Path= /url-getprotocol-method-in-java-with-examples

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程