Home >Java >javaTutorial >Java.net URI
Java.net package offers a class URI that contains methods for URI instance creation from its components or parsing string form of those components to access and retrieve several components of a URI instance.
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
The uniform Resource Identifier has a sequence of characters that helps in a specific resource identification. Moreover, it helps in the interaction of the resource representation over the network with the help of certain protocols. More details on this particular class will be discussed in the below sections.
Syntax
Below is the syntax of the java.net URI class.
scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]
As already discussed, URI is known as the Uniform Resource Locator, which helps identify a web resource such as images, Cascade Style Sheet files, videos, etc., by using the locations and names. Here, the location is the mechanism to access or retrieve specific resources. For example, HTTP://, FTP://, etc are the locations. In addition to that, “name” is the globally specific name of the particular resource. Let us see an example, where the following web address: https://samplepath/path/to/video.mkv retrieves a video. Here, sample-path/path/to/video.mkv section of this particular address is the URN or name.
The following are the five different constructors of URI.
Below are the different methods of the URI class that performs several operations.
Now, let us see a sample program of java.net uri class.
Java program to demonstrate several methods in the class java.net.uri. //Java Program to demonstrate methods of URI class
Code:
import java.net.*; class URIexample { public static void main(String[] args) throws Exception { String str = "https://www.google.com/search?q=educba+treemap&oq=educba+treemap+&aqs=chrome..69i57j69i60.12298j1j7&sourceid=chrome&ie=UTF-8"; // Creation of new URI by parsing the string URI exampleuri = new URI(str); //different methods of <u>uri</u> class System.out.println("Host of the URI is: " + exampleuri.getHost()); System.out.println("Port of the URI is: = " + exampleuri.getPath()); System.out.println("Raw Path of the URI is: = " + exampleuri.getRawPath()); System.out.println("Path of the URI is: = " + exampleuri.getPath()); System.out.println("Query of the URI is: = " + exampleuri.getQuery()); System.out.println("Raw Query of the URI is: = " + exampleuri.getRawQuery()); System.out.println("Fragment of the URI is: = " + exampleuri.getFragment()); System.out.println("Raw Fragment of the URI is: = " + exampleuri.getRawFragment()); //another uri in order to demonstrate the method compareTo and equals URI exampleuri2 = new URI(str + "fr"); System.out.println("CompareTo exampleuri2=" + exampleuri.compareTo(exampleuri2)); System.out.println("Equals of the URI is: = " + exampleuri.equals(exampleuri2)); System.out.println("Hashcode of the URI is: " + exampleuri.hashCode()); System.out.println("toString of the URI is: " + exampleuri.toString()); System.out.println("toASCIIString of the URI is: " + exampleuri.toASCIIString()); } }
Output: In this program, different methods of java.net.uri class are used and displayed results based on that.
The above is the detailed content of Java.net URI. For more information, please follow other related articles on the PHP Chinese website!