All About Class URL in Java

0
37
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Class URL in Java
URL

All About Class URL in Java

Class URL represents a uniform resource locator-a pointer to a resource on the world wide web such as webpage or FTP directory.A resource can be sometimes as simple as a file or a directory or it can be reference to a more complicated object,such as a query to a database or to a search engine.

The URL can be broken down into parts like:
protocol://host:port/path?query=#ref
Example of protocol can be HTTP,HTTPS,FTP and File. The path can also be referred as filename and the host can be referred as authority.

In case the URL does not specify the port, the default port for the corresponding protocol is used. Like HTTP default port is 80.

 

The class structure of URL is given as:


public final class java.net.URL extends java.lang.Object{
//constructors
public URL();//creates a URL object from the String representation.This constructor is equivalent 
//to a call to the two argument constructor with null first arguments.Throws MalformedURLException
//if the string specifies an Unknown protocol.
public URL(String url);//creates a URL from the given String.
public URL(String protocol,String host,int port,String file);//Creates an URL from the specified 
//protocol,host,port number and file. Throws MalformedURLException if the string specifies
//an Unknown protocol.
public URL(String protocol,String host,String file);//Creates an absolute URL from the specified
//protocol,host and file. Throws MalformedURLException if the string specifies
//an Unknown protocol.
public URL(URL context,String spec);//Creates a URL by parsing the String specification within a 
//specific context.If the context argument is not null and teh spec argument is a particular URL
//specification,then any of the Strings missing components are inherited from the context argument.
//throws  MalformedURLException  if no protocol is specified or an unknown protocol is found.
//Methods
public boolean equals(Object obj);
public final Object getContext();//returns the context of the URL.
public String getFile();//Returns the filename of the URL.
public String getPath();//returns the path of the URL.
public String getQuery();//returns the query part of the URL.
public String getAuthority();//returns the authority of the URL.
public String getHost();//returns the host of the URL.
public int getPort();//returns the port of the URL.
public int getDefaultPort();//returns the default port of the protocol of the URL.
public String getProtocol();//returns the protocol of the URL.
public String getRef();//returns the reference part of the URL.
public int hashCode();
public URLConnection openConnection() throws IOException;
//Opens a connection to the URL,allowing a client to communicate with the resource.
public final InputStream openStream();
public boolean sameFile(URL other);
public static void setURLStreamHandlerFactory(URLStreamHandlerFactory fac);
public String toExternalForm();
public String toString();
}
 
Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here