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.
Function Signature
public URI toURI()
Syntax
url.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:
Example 1:
import java.net.*;
class GFG {
public static void main(String args[])
{
URL url = null ;
URI uri = null ;
try {
System.out.println( "URL: " + url);
uri = url.toURI();
System.out.println( "URI: " + uri);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
URL: https://www.geeksforgeeks.org
URI: https://www.geeksforgeeks.org
Example 2:
import java.net.*;
class GFG {
public static void main(String args[])
{
URL url = null ;
URI uri = null ;
try {
System.out.println( "URL: " + url);
uri = url.toURI();
System.out.println( "URI: " + uri);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
URL: https:// www.geeksfor>geeks.com
java.net.URISyntaxException:
Illegal character in authority at index 8:
https:// www.geeksfor>geeks.com
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Nov, 2019
Like Article
Save Article