The URL class provides a simple, concise API to access information over the Internet.
URL Class Methods
| Method | Description |
|---|---|
| String getAuthority() | Returns the authority part of the URL. |
| int defaultPort() | Returns the default port number of the protocol associated with given URL. |
| String getFile() | Returns the file name of the given URL. |
| String getHost() | Returns the host name of the URL. |
| String getPath() | Returns the complete path of the given URL. |
| int getPort() | Returns the port number of the given URL. If it is not applicable, it returns -1. |
| String getQuery() | Returns the query part of the given URL. |
| String getProtocol() | Returns the name of the protocol of the given URL. |
Example: Illustrating different methods of URL class
import java.net.*;
public class URLDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL url = new URL("http://www.tutorialride.com/java-technologies.htm");
System.out.println("URL: " + url.toString());
System.out.println("Protocol: "+url.getProtocol());
System.out.println("path: " + url.getPath());
System.out.println("Port: "+url.getPort());
System.out.println("Host: "+url.getHost());
System.out.println("Authority: " + url.getAuthority());
System.out.println("File: "+url.getFile());
System.out.println("Query: "+url.getQuery());
System.out.println("HashCode: "+url.hashCode());
System.out.println("External form: "+url.toExternalForm());
}
}
Output:
URL: http://www.tutorialride.com/java-technologies.htm
Protocol: http
path: /java-technologies.htm
Port: -1
Host: www.tutorialride.com
Authority: www.tutorialride.com
File: /java-technologies.htm
Query: null
HashCode: -491067311
External form: http://www.tutorialride.com/java-technologies.htm


