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/
The URL contains three or four parts, usually many forms of the URL contains three parts as in the above URL image.
The URL class contains constructors and some function as a URL class member function.
Here are the following examples mentioned below:
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); } } }
Output:
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); } } }
Ouput:
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); } } }
Output:
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!