The getURI() function of URL class converts the URL object to a URI object. Any URL which compiles with RFC 2396 can be converted to URI. URLs which are not in the specified format will generate an error if converted to URI format.
public URI toURI()
Parameter: This method do not accept any parameter.
Return type: This function returns a URI object which is converted from this URL object.
Exception: This function throws URISyntaxException if this URL is not formatted strictly according to RFC2396 and cannot be converted to a URI.
Below examples will illustrate the use of toURI() function:
URL: https://www.geeksforgeeks.org URI: https://www.geeksforgeeks.org
URL: https:// www.geeksfor>geeks.com java.net.URISyntaxException: Illegal character in authority at index 8: https:// www.geeksfor>geeks.com
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.