Skip to main content Skip to docs navigation
TechSpiderTutorials

java.net.URL class in Java

java.net.URL class

The java.net.URL class is a powerful tool in Java for handling Uniform Resource Locators (URLs). It provides methods for creating, manipulating, and accessing resources over the internet. This tutorial will guide you through the key features and usage of the java.net.URL class.

What is a URL?

A URL (Uniform Resource Locator) is a reference to a resource on the internet. It specifies the location of the resource and the protocol used to access it, such as HTTP, HTTPS, FTP, etc.

Creating Objects

1.Constructor with a URL string:
URL url1 = new URL("http://www.example.com");
2. Constructor with protocol, host, and file:
URL url2 = new URL("http", "www.example.com", "/path/to/resource");
3. Constructor with protocol, host, port, and file:
URL url3 = new URL("http", "www.example.com", 80, "/path/to/resource");
Constructor with a URL and a file path:
URL baseUrl = new URL("http://www.example.com");
URL url4 = new URL(baseUrl, "/path/to/resource");

java.net.URL Methods

public String getAuthority()

Gets the authority part of this URL.


public Object getContent()

Gets the contents of this URL.


public Object getContent(Class[] classes)

Gets the contents of this URL.


public int getDefaultPort()

Gets the default port number of the protocol associated with this URL.


public String getFile()

Gets the file name of this URL.


public String getHost()

public Gets the host name of this URL, if applicable.


public String getPath()

Gets the path part of this URL.


public int getPort()

Gets the port number of this URL.


public String getProtocol()

Gets the protocol name of this URL.


public String getQuery()

Gets the query part of this URL.


public String getRef()

Gets the anchor (also known as the "reference") of this URL.


public String getUserInfo()

Gets the userInfo part of this URL.


public int hashCode()

Creates an integer suitable for hash table indexing.


public URLConnection openConnection()

Returns a URLConnection instance that represents a connection to the remote object referred to by the URL.


public URLConnection 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.


public InputStream openStream()

Opens a connection to this URL and returns an InputStream for reading from that connection.


java.net.URL Class Example

import java.io.*;
import java.net.*;

public class URLInfo
{
 public static void main(String args[])
  {
    try
	 {
 URL url1=new URL("http://www.yahoo.com/users/index.html");

 URL url2=new URL("http://yahoo.com:90/index.html#aboutus");

URL url3=new URL("http://www.microsoft.com/index.asp?soft=msdn&uid=techspider");

System.out.println("Url1:Protocal:"+url1.getProtocol());
System.out.println("url1:Default Port number is..."+url1.getDefaultPort() );
System.out.println("Url1: Port :"+url1.getPort());
System.out.println("Url1: File :"+url1.getFile());
System.out.println("Url1:Host :"+url1.getHost());

System.out.println("Url2:Port "+url2.getPort());
System.out.println("Url2: Reference..>"+url2.getRef());

System.out.println("Url3: Query String is:"+url3.getQuery());

      }catch(MalformedURLException uhe)
	   {
	     System.out.println(uhe);
		}
  }
}

Output:

Url1:Protocal:http
url1:Default Port number is...80
Url1: Port :-1
Url1: File :/users/index.html
Url1:Host :www.yahoo.com
Url2:Port 90
Url2: Reference..>aboutus
Url3: Query String is:soft=msdn&uid=techspider