java.net.URL Class in Java
URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc.
The typical URL may look like
http://www.example.com:80/index.html
The URL has the following parts:
- Protocol: In this case the protocol is HTTP, It can be HTTPS in some cases
- Hostname: Hostname represent the address of the machine on which resource is located, in this case, www.example.com
- Port Number: It is an optional attribute. If not specified then it returns -1. In the above case, the port number is 80.
- Resource name: It is the name of a resource located on the given server that we want to see
You can learn more about the URL here.
The Class structure of the URL is as shown below:
public final class java.net.URL extends java.lang.Object
The Following are constructors provided by the URL class.
Constructor | Explanation |
|---|---|
| public URL(String url ) | This constructor creates an object of URL class from given string representation |
| public URL(String protocol, String host, int port, String file) | This constructor creates an object of URL from the specified protocol, host, port number, and file. |
| public URL(String protocol, String host, String file) | This constructor creates an object of URL from the specified protocol, port number, and file. The default port number is used in this case. |
| public URL(URL context, String src) | This constructor creates an instance of a URL by parsing the given src with the specified handler within a given context. |
Methods Provided by URL Class:
| Method | Explanation |
|---|---|
| equals(Object obj) | This method compares this URL for equality with another object. |
| getAuthority() | This method gets the authority part of this URL. |
| getContent() | This method gets the contents of this URL. |
| getContent(Class[] classes) | This method gets the contents of this URL. |
| getDefaultPort() | This method gets the default port number of the protocol associated with this URL. |
| getFile() | This method gets the file name of this URL. |
| getHost() | This method gets the hostname of this URL, if applicable. |
| getPath() | This method gets the path part of this URL. |
| getPort() | This method gets the port number of this URL. |
| getProtocol() | This method gets the protocol name of this URL. |
| getQuery() | This method gets the query part of this URL. |
| getRef() | This method gets the anchor (also known as the "reference") of this URL. |
| getUserInfo() | This method gets the userInfo part of this URL. |
| hashCode() | This method creates an integer suitable for hash table indexing. |
| openConnection() | This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL. |
| openConnection(Proxy proxy) | Same as openConnection(), except that the connection will be made through the specified proxy; Protocol handlers that do not support proxing will ignore the proxy parameter and make a normal connection. |
| openStream() | This method opens a connection to this URL and returns an InputStream for reading from that connection. |
| sameFile(URL other) | This method compares two URLs, excluding the fragment component. |
| set(String protocol, String host, int port, String file, String ref) | This method sets the fields of the URL. |
| set(String protocol, String host, int port, String authority, String userInfo, String path, String query, String ref) | This method sets the specified 8 fields of the URL. |
| setURLStreamHandlerFactory(URLStreamHandlerFactory fac) | This method sets an application's URLStreamHandlerFactory. |
| toExternalForm() | This method constructs a string representation of this URL. |
| toString() | This method constructs a string representation of this URL. |
| toURI() | This method returns a URI equivalent to this URL. |
Demonstration 1
In this demonstration, we are going to create an object of URL class for a given String URL, and we are going to print hostname, protocol, filename and Port number for given URL.
// importing package required
import java.net.URL;
import java.util.Scanner;
class GFG {
public static void main(String[] args)
{
String url
= "https://www.geeksforgeeks.org/variables-in-java/";
// Calling method to get URL info
getUrlInfo(url);
}
static void getUrlInfo(String url_string)
{
// Creating object of URL class
try {
URL url = new URL(url_string);
System.out.println("Hostname: "
+ url.getHost());
System.out.println("Port Number: "
+ url.getPort());
System.out.println("File name: "
+ url.getFile());
System.out.println("Protocol: "
+ url.getProtocol());
}
catch (Exception e) {
}
}
}
Output
Hostname: www.geeksforgeeks.org Port Number: -1 File name: /variables-in-java/ Protocol: https