URL getPath() method in Java with Examples (original) (raw)

Last Updated : 31 Dec, 2018

The getPath() function is a part of URL class. The function getPath() returns the Path name of a specified URL.

Function Signature:

public String getPath()

Syntax:

url.getPath()

Parameter: This function does not require any parameter
Return Type: The function returns String Type

Below programs illustrates the use of getPath() function:

Example 1: Given a URL we will get the Path using the getPath() function.

import java.net.*;

class Solution {

`` public static void main(String args[])

`` {

`` URL url = null ;

`` try {

`` url = new URL(

`` "<https://> www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples/" );

`` String _Path = url.getPath();

`` System.out.println( "URL = " + url);

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

`` }

`` catch (Exception e) {

`` System.out.println(e);

`` }

`` }

}

Output:

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

Example 2: Now see how getPath() is different from getFile(). getPath() will exclude the query but getFile() will include the query

import java.net.*;

class Solution {

`` public static void main(String args[])

`` {

`` URL url = null ;

`` try {

`` url = new URL(

`` "<https://> www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol" );

`` String _Path = url.getPath();

`` System.out.println( "URL = " + url);

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

`` }

`` catch (Exception e) {

`` System.out.println(e);

`` }

`` }

}

Output:

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