Home > Java > javaTutorial > body text

Java URL Class

王林
Release: 2024-08-30 15:43:11
Original
1029 people have browsed it

The java URL class is used to specify an URL. The URL class in java is a getaway to access any resource available on the web or internet. URL is a Uniform Resource Locator that points to the resource like file, directory, or image on the World Wide Web(www). The URL class in java is built in the java.net.URL package of java.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The URL specifies the address of a resource on the World Wide Web. The URL is unique to reach resources on the World Wide Web. Consider an URL example as https://www.educba.com/what-is-java/

Java URL Class

Components of Java URL Class

The URL contains three or four parts, usually many forms of the URL contains three parts as in the above URL image.

  1. Protocol – Protocol sub part of the URL specifies the name of the protocol as here protocol is HTTP.
  2. Hostname – Hostname or IP Address orServer name of URL specifies the name of the machine or server, as here the hostname is www.educba.com.
  3. Port Number – Port Number of URL is an optional part, which specifies the logical address of each resource that uses the Internet to communicate. Each resource is allocated a 16-bit integer port number, as here we do not have the port number. If the port number is not present then it returns -1.
  4. File Name – File name or document name or directory name specifies the Pathname to resource or file on the server, as here file name is “what-is-java”.

Constructors and Functions of Java URL Class

The URL class contains constructors and some function as a URL class member function.

Constructors

  1. URL(String url) – This constructor creates a URL object from the given string url.
  2. URL(String protocol, String host, String file) – This constructor creates a URL object from the specified protocol, host, and file.
  3. URL(String protocol, String host, intportno, String file) – This constructor creates a URL object from the specifiedprotocol name, hostname, port number, and file name.
  4. URL(String protocol, String host, intportno, String file, URLStreamHandler handler) – This constructor creates a URL object from the specified protocol, host, port number, file, and handler.
  5. URL(URL context, String url) – This constructor creates a URL object from the specifiedurlin in the given context.
  6. URL(URL context, String url, URLStreamHandler handler) – This constructor creates a URL object from the specifiedurlin the specified context with the given handler.

Functions

  1. public String getProtocol() – This function gives the protocol used by the URL.
  2. public String getHost() – This function gives the hostname used by the URL.
  3. public String getPort() – This function gives the Port Number used by the URL.
  4. public String getFile() – This function gives the file name.
  5. public String getAuthority() – This function gives the authority of an URL if empty return null.
  6. public String toString() – This function gives the URL representation in string.
  7. public String getQuery() – This function gives the query of the URL. Query part in URL is present after ‘?’.
  8. public String getDefaultPort() – This function gives the URL default port.
  9. publicURLConnectionopenConnection() – This function gives an object of URLConnectionof the URL.
  10. public String getPath() – This function gives the path of the URL if empty return null.
  11. publicboolean equals(Object obj) – This function compares the two URL object by the given URL object.
  12. public Object getContent() – This function gives the URL content.
  13. public String getRef() – This function gives the reference of the URL. The reference part in the URL is present after ‘#’.
  14. public URI toURI() – This function gives the URI of the URL.

Examples

Here are the following examples mentioned below:

Example #1

Next, we write the java code to understand the URL class more clearly with the following example where we create an URL object by using the URL class constructor and pass the URL, as below –

Code:

import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{   try{
URL url=new URL("https://www.educba.com/what-is-java/");
System.out.print("\nThe URL is : "+url.toString()+"\nThe Protocol is : "+url.getProtocol());
System.out.print("\nTheHostName is : "+url.getHost()+"\nThe Port No is : "+url.getPort());
System.out.print("\nThe Default port is : " +url.getDefaultPort());
System.out.print("\nThe File Name is : "+url.getFile()+"\nThe Path is : " +url.getPath());
System.out.print("\nThe Query is : " +url.getQuery()+"\nThe Reference is : " +url.getRef());
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Copy after login

Output:

Java URL Class

Example #2

Next, we write the java code to understand the URL class where we create an URL object bypassing the query URL, as below –

Code:

import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{   try{
URL url=new URL("https://www.google.com/search?q=educba+learn+java&oq=educba+learn+java&aqs=chrome..69i57j69i60.19364j0j9&sourceid=chrome&ie=UTF-8");
System.out.print("\nThe URL is : "+url.toString()+"\nThe Protocol is : "+url.getProtocol());
System.out.print("\nTheHostName is : "+url.getHost()+"\nThe Port No is : "+url.getPort());
System.out.print("\nThe Default port is : " +url.getDefaultPort());
System.out.print("\nThe File Name is : "+url.getFile()+"\nThe Path is : " +url.getPath());
System.out.print("\nThe Query is : " +url.getQuery()+"\nThe Reference is : " +url.getRef());
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Copy after login

Ouput:

Java URL Class

Example #3

Next, we write the java code to understand the URL class where we create an URL object and read the data available in that URL, as below –

Code:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
public class Demo
{
public static void main(String[] arg)
{
String data;
try{
URL url=new URL("https://www.educba.com/what-is-java/");
System.out.println("The URL is : "+url.toString());
System.out.println("The Protocol is : "+url.getProtocol());
System.out.println("The HostName is : "+url.getHost());
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("The data at this URL is : ");
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
}catch(Exception e)
{
System.out.println("Error : "+e);
}
}
}
Copy after login

Output:

Java URL Class

Conclusion

The java URL class is a built-in class in java, can be accessed from java.net.URL package. This class is used to specify an URL or to create a URL object, which farther can be to access the resource available on the web.

The above is the detailed content of Java URL Class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template