Java URL is a class in the Java programming language that represents a Uniform Resource Locator (URL) reference. It is used to create, read, and manipulate URLs, which are used to locate resources on the internet or within a local network. The URL class provides methods for accessing various components of a URL, such as the protocol, host, port, path, and query string. It can also be used to open a connection to a URL and retrieve data from it. Here is an example of how to create a URL object in Java:
import java.net.*; public class URLExample { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com"); System.out.println("Protocol: " + url.getProtocol()); System.out.println("Host: " + url.getHost()); System.out.println("Port: " + url.getPort()); System.out.println("Path: " + url.getPath()); System.out.println("Query: " + url.getQuery()); } }
This code creates a URL object for the “https://www.example.com” URL and then prints out various components of the URL using the methods provided by the URL class.
Constructors of Java URL class:
The Java URL class provides several constructors that can be used to create a URL object. Here are the available constructors:
URL(String spec)
– Creates a URL object from the specified string representation of a URL. For example,URL url = new URL("https://www.example.com");
URL(String protocol, String host, int port, String file)
– Creates a URL object from the specified protocol, host, port, and file components. For example,URL url = new URL("https", "www.example.com", 443, "/index.html");
URL(String protocol, String host, int port, String file, URLStreamHandler handler)
– Same as the previous constructor, but allows a custom URLStreamHandler to be specified. This is rarely used.URL(URL context, String spec)
– Creates a URL object from a base URL and a relative URL. The base URL is specified by the context parameter, and the relative URL is specified by the spec parameter. For example,URL url = new URL(new URL("https://www.example.com"), "/index.html");
URL(URL context, String spec, URLStreamHandler handler)
– Same as the previous constructor, but allows a custom URLStreamHandler to be specified. This is rarely used.
Note that the constructors can throw a MalformedURLException if the specified URL string is invalid.
Commonly used methods of Java URL class:
The Java URL class provides a variety of methods for accessing and manipulating the various components of a URL. Here are some commonly used methods:
openStream()
– Returns an input stream that can be used to read the contents of the resource specified by the URL.openConnection()
– Returns a URLConnection object that can be used to further customize the connection to the resource specified by the URL.getProtocol()
– Returns the protocol component of the URL as a string (e.g., “http”, “https”, “ftp”).getHost()
– Returns the host component of the URL as a string (e.g., “www.example.com“).getPort()
– Returns the port component of the URL as an int. If no port is specified in the URL, it returns the default port for the protocol (e.g., 80 for HTTP).getPath()
– Returns the path component of the URL as a string (e.g., “/index.html”).getQuery()
– Returns the query component of the URL as a string (e.g., “q=java+url”).getFile()
– Returns the file component of the URL as a string, which includes the path and query components (e.g., “/index.html?q=java+url”).toString()
– Returns the string representation of the URL.toURI()
– Returns a URI object representing the same URL. This can be useful for performing additional URI-specific operations.
Note that the methods may throw various exceptions, such as IOException or URISyntaxException, depending on the operation being performed.
Example of Java URL class:
Sure! Here is an example of using the Java URL class to retrieve and display the contents of a webpage:
import java.net.*; import java.io.*; public class URLExample { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } }
This code creates a URL object for the “https://www.example.com” URL, opens a connection to the URL, and reads the contents of the webpage using a BufferedReader. It then prints each line of the webpage to the console. Note that this code uses a try-with-resources block to automatically close the BufferedReader after it is used. Also note that the code may throw an IOException if there is an error reading from the stream.