The getPath()
method of the URL
class in Java can be used to get the path part of the specified
The image below shows the different parts of a URL.
The
URL
class is present in thejava.net
package.
The syntax of the getPath()
method is shown below.
public String getPath()
The getPath()
method doesn’t take any parameters.
The return type of this method is a string
. If the URL doesn’t have a path, then an empty string is returned.
The code below shows how to use the getPath()
method in Java.
import java.net.URL;class GetPathExample {public static void main( String args[] ) {try {URL url= new URL("https:// www.educative.io/user/profile/view");//Get PathString path=url.getPath();System.out.println("The URL is : "+url);System.out.println("\nThe Path of the URL is : "+ path);} catch (Exception e) {System.out.println(e);}}}
In the code above:
URL
object.URL url= new URL("https:// www.educative.io/user/profile/view");
getPath()
method gets the path of the URL
object.String path = url.getPath();