URI(String): This method is available in the java.net.URI class of Java.
Syntax:
java.net.URI.URI(String str) throws URISyntaxException
This method takes one argument. This method constructs a URI by parsing the given string.
Note:
1. An empty authority component is permitted as long as it is followed by a non-empty path, a query component, or a fragment component. If the authority component is empty then the user information, host, and port components are undefined.
2. Empty relative paths are permitted. The primary consequence of this deviation is that a standalone fragment such as "#foo" parses as a relative URI with an empty path and the given fragment, and can be usefully resolved against a base URI.
3. IPv4 addresses in host components are parsed rigorously, as specified by RFC 2732: Each element of a dotted-quad address must contain no more than three decimal digits. Each element is further constrained to have a value no greater than 255.
4. Hostnames in host components that comprise only a single domain label are permitted to start with an alphanum character. The consequence of this deviation is that the authority component of a hierarchical URI such as s://123, will parse as a server-based authority.
5. IPv6 addresses are permitted for the host component. An IPv6 address must be enclosed in square brackets ('[' and ']'). The IPv6 address itself must parse according to RFC 2373. IPv6addresses are further constrained to describe no more than sixteen bytes of address information, a constraint implicit in RFC 2373 but not expressible in the grammar.
6. Characters in the other category are permitted wherever RFC 2396 permits escaped octets, that is, in the user information, path, query, and fragment components, as well as in the authority component if the authority is registry-based. This allows URIs to contain Unicode characters beyond those in the US-ASCII character set.
Parameters: One parameter is required for this method.
str: The string to be parsed into a URI.
Throws:
1. NullPointerException - If str is null.
2. URISyntaxException - If the given string violates RFC 2396, as augmented by the above deviations
Approach 1: When no exception
Java
package com.URI;import java.net.URI;import java.net.URISyntaxException;public class URI1 {public static void main(String[] args) throws URISyntaxException {String str = "/hello";URI uri = new URI(str);System.out.println(uri);}}
Output:
/hello
Java
package com.URI;import java.net.URI;import java.net.URISyntaxException;public class URI1 {public static void main(String[] args) throws URISyntaxException {String str = null;URI uri = new URI(str);System.out.println(uri);}}
Output:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "this.input" is null at java.base/java.net.URI$Parser.parse(URI.java:3154) at java.base/java.net.URI.<init>(URI.java:623) at com.URI.URI1.main(URI1.java:10)
Approach 3: URISyntaxException
Java
package com.URI;import java.net.URI;import java.net.URISyntaxException;public class URI1 {public static void main(String[] args) throws URISyntaxException {String str = "\\hello";URI uri = new URI(str);System.out.println(uri);}}
Output:
Exception in thread "main" java.net.URISyntaxException: Illegal character in path at index 0: \hello at java.base/java.net.URI$Parser.fail(URI.java:2963) at java.base/java.net.URI$Parser.checkChars(URI.java:3134) at java.base/java.net.URI$Parser.parseHierarchical(URI.java:3216) at java.base/java.net.URI$Parser.parse(URI.java:3175) at java.base/java.net.URI.<init>(URI.java:623) at com.URI.URI1.main(URI1.java:10)
Some other methods of URI class
URI(String): This method constructs a URI by parsing the given string.
URI(String, String, String): This method constructs a URI from the given components. A component may be left undefined by passing null.
URI(String, String, String, String): This method constructs a hierarchical URI from the given components.
URI(String, String, String, String, String): This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter.
URI(String, String, String, int, String, String, String): This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter or, in the case of the port parameter, bypassing -1.
compareTo(URI): This method compares this URI to another object, which must be a URI.
URI.create(String): This method creates a URI by parsing the given string.
equals(Object): This method tests this URI for equality with another object.
getAuthority(): This method returns the decoded authority component of this URI.
getFragment(): This method returns the decoded fragment component of this URI.
getHost(): This method returns the host component of this URI.
getPath(): This method returns the decoded path component of this URI.
getPort(): This method returns the port number of this URI.
getQuery(): This method returns the decoded query component of this URI.
getRawAuthority(): This method returns the raw authority component of this URI.
getRawFragment(): This method returns the raw fragment component of this URI.
getRawPath(): This method returns the raw path component of this URI.
getRawQuery(): This method returns the raw query component of this URI.
getRawSchemeSpecificPart(): This method returns the raw scheme-specific part of this URI. The scheme-specific part is never undefined, though it may be empty.
getRawUserInfo(): This method returns the raw user-information component of this URI.
getScheme(): This method returns the scheme component of this URI.
hashCode(): This method returns a hash code value for this URI.
isAbsolute(): This method tells whether or not this URI is absolute.
isOpaque(): This method tells whether or not this URI is opaque.
normalize(): This method normalizes this URI's path.
parseServerAuthority(): This method attempts to parse this URI's authority component if defined, into user information, host, and port components.
relativize(URI): This method relativizes the given URI against this URI.
resolve(String): This method constructs a new URI by parsing the given string and then resolving it against this URI.
resolve(URI): This method resolves the given URI against this URI
toASCIIString(): This method returns the content of this URI as a US-ASCII string.
toString(): This method returns the content of this URI as a string.
toURL(): This method constructs a URL from this URI.
No comments:
Post a Comment