Updated April 12, 2023
Introduction to Java URL Class
The java URL class is used to specify an URL. The URL class in java is a getaway to access any resource available on the web or internet. URL is a Uniform Resource Locator that points to the resource like file, directory, or image on the World Wide Web(www). The URL class in java is built in the java.net.URL package of java.
The URL specifies the address of a resource on the World Wide Web. The URL is unique to reach resources on the World Wide Web. Consider an URL example as https://www.educba.com/what-is-java/
Components of Java URL Class
The URL contains three or four parts, usually many forms of the URL contains three parts as in the above URL image.
- Protocol – Protocol sub part of the URL specifies the name of the protocol as here protocol is HTTP.
- Hostname – Hostname or IP Address orServer name of URL specifies the name of the machine or server, as here the hostname is www.educba.com.
- Port Number – Port Number of URL is an optional part, which specifies the logical address of each resource that uses the Internet to communicate. Each resource is allocated a 16-bit integer port number, as here we do not have the port number. If the port number is not present then it returns -1.
- File Name – File name or document name or directory name specifies the Pathname to resource or file on the server, as here file name is “what-is-java”.
Constructors and Functions of Java URL Class
The URL class contains constructors and some function as a URL class member function.
Constructors
- URL(String url) – This constructor creates a URL object from the given string url.
- URL(String protocol, String host, String file) – This constructor creates a URL object from the specified protocol, host, and file.
- URL(String protocol, String host, intportno, String file) – This constructor creates a URL object from the specifiedprotocol name, hostname, port number, and file name.
- URL(String protocol, String host, intportno, String file, URLStreamHandler handler) – This constructor creates a URL object from the specified protocol, host, port number, file, and handler.
- URL(URL context, String url) – This constructor creates a URL object from the specifiedurlin in the given context.
- URL(URL context, String url, URLStreamHandler handler) – This constructor creates a URL object from the specifiedurlin the specified context with the given handler.
Functions
- public String getProtocol() – This function gives the protocol used by the URL.
- public String getHost() – This function gives the hostname used by the URL.
- public String getPort() – This function gives the Port Number used by the URL.
- public String getFile() – This function gives the file name.
- public String getAuthority() – This function gives the authority of an URL if empty return null.
- public String toString() – This function gives the URL representation in string.
- public String getQuery() – This function gives the query of the URL. Query part in URL is present after ‘?’.
- public String getDefaultPort() – This function gives the URL default port.
- publicURLConnectionopenConnection() – This function gives an object of URLConnectionof the URL.
- public String getPath() – This function gives the path of the URL if empty return null.
- publicboolean equals(Object obj) – This function compares the two URL object by the given URL object.
- public Object getContent() – This function gives the URL content.
- public String getRef() – This function gives the reference of the URL. The reference part in the URL is present after ‘#’.
- public URI toURI() – This function gives the URI of the URL.
Examples
Here are the following examples mentioned below:
Example #1
Next, we write the java code to understand the URL class more clearly with the following example where we create an URL object by using the URL class constructor and pass the URL, as below –
Code:
import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{ try{
URL url=new URL("https://www.educba.com/what-is-java/");
System.out.print("\nThe URL is : "+url.toString()+"\nThe Protocol is : "+url.getProtocol());
System.out.print("\nTheHostName is : "+url.getHost()+"\nThe Port No is : "+url.getPort());
System.out.print("\nThe Default port is : " +url.getDefaultPort());
System.out.print("\nThe File Name is : "+url.getFile()+"\nThe Path is : " +url.getPath());
System.out.print("\nThe Query is : " +url.getQuery()+"\nThe Reference is : " +url.getRef());
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Output:
Example #2
Next, we write the java code to understand the URL class where we create an URL object bypassing the query URL, as below –
Code:
import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{ try{
URL url=new URL("https://www.google.com/search?q=educba+learn+java&oq=educba+learn+java&aqs=chrome..69i57j69i60.19364j0j9&sourceid=chrome&ie=UTF-8");
System.out.print("\nThe URL is : "+url.toString()+"\nThe Protocol is : "+url.getProtocol());
System.out.print("\nTheHostName is : "+url.getHost()+"\nThe Port No is : "+url.getPort());
System.out.print("\nThe Default port is : " +url.getDefaultPort());
System.out.print("\nThe File Name is : "+url.getFile()+"\nThe Path is : " +url.getPath());
System.out.print("\nThe Query is : " +url.getQuery()+"\nThe Reference is : " +url.getRef());
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Ouput:
Example #3
Next, we write the java code to understand the URL class where we create an URL object and read the data available in that URL, as below –
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{
String data;
try{
URL url=new URL("https://www.educba.com/what-is-java/");
System.out.println("The URL is : "+url.toString());
System.out.println("The Protocol is : "+url.getProtocol());
System.out.println("The HostName is : "+url.getHost());
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The data at this URL is : ");
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Output:
Conclusion
The java URL class is a built-in class in java, can be accessed from java.net.URL package. This class is used to specify an URL or to create a URL object, which farther can be to access the resource available on the web.
Recommended Articles
This is a guide to Java URL Class. Here we discuss the Components, Constructors, and Functions of Java URL Class with the respective examples. You may also have a look at the following articles to learn more –