1
+ /*
2
+
3
+ The Java URL class represents an URL.
4
+ URL is an acronym for Uniform Resource Locator.
5
+ It points to a resource on the World Wide Web.
6
+
7
+ A URL contains many information:
8
+
9
+ Protocol:
10
+ Server name or IP Address:
11
+ Port Number: It is an optional attribute.
12
+ File Name or directory name:
13
+
14
+
15
+ Constructors of Java URL class
16
+ URL(String spec)
17
+
18
+
19
+ Creates an instance of a URL from the String representation.
20
+
21
+ URL(String protocol, String host, int port, String file)
22
+
23
+ Creates an instance of a URL from the given protocol, host, port number, and file.
24
+
25
+ URL(String protocol, String host, int port, String file, URLStreamHandler handler)
26
+
27
+ Creates an instance of a URL from the given protocol, host, port number, file, and handler.
28
+
29
+ URL(String protocol, String host, String file)
30
+
31
+ Creates an instance of a URL from the given protocol name, host name, and file name.
32
+
33
+ URL(URL context, String spec)
34
+
35
+ Creates an instance of a URL by parsing the given spec within a specified context.
36
+
37
+ URL(URL context, String spec, URLStreamHandler handler)
38
+
39
+ Creates an instance of a URL by parsing the given spec with the specified handler within a given context.
40
+
41
+ Method Description
42
+ public String getProtocol() it returns the protocol of the URL.
43
+ public String getHost() it returns the host name of the URL.
44
+ public String getPort() it returns the Port Number of the URL.
45
+ public String getFile() it returns the file name of the URL.
46
+ public String getAuthority() it returns the authority of the URL.
47
+ public String toString() it returns the string representation of the URL.
48
+ public String getQuery() it returns the query string of the URL.
49
+ public String getDefaultPort() it returns the default port of the URL.
50
+ public URLConnection openConnection() it returns the instance of URLConnection i.e. associated with this URL.
51
+ public boolean equals(Object obj) it compares the URL with the given object.
52
+ public Object getContent() it returns the content of the URL.
53
+ public String getRef() it returns the anchor or reference of the URL.
54
+ public URI toURI() it returns a URI of the URL.
55
+
56
+ */
57
+
58
+ //URLDemo.java
59
+ import java .net .*;
60
+ public class URLDemo {
61
+ public static void main (String [] args ) {
62
+ try {
63
+ URL url =new URL ("https://www.google.com/search?q=javatpoint&oq=javatpoint&sourceid=chrome&ie=UTF-8" );
64
+
65
+ System .out .println ("Protocol: " +url .getProtocol ());
66
+ System .out .println ("Host Name: " +url .getHost ());
67
+ System .out .println ("Port Number: " +url .getPort ());
68
+ System .out .println ("Default Port Number: " +url .getDefaultPort ());
69
+ System .out .println ("Query String: " +url .getQuery ());
70
+ System .out .println ("Path: " +url .getPath ());
71
+ System .out .println ("File: " +url .getFile ());
72
+
73
+ }
74
+ catch (Exception e ) {
75
+ System .out .println (e );
76
+ }
77
+ }
78
+ }
0 commit comments