URL toURI() method in Java with Examples Last Updated : 07 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Java // Java program to convert URL to URI import java.net.*; class GFG { public static void main(String args[]) { // url and uri objects URL url = null; URI uri = null; try { // create a URL url = new URL("https://www.geeksforgeeks.org"); // display the URL System.out.println("URL: " + url); // convert the URL to URI uri = url.toURI(); // display the URI System.out.println("URI: " + uri); } // if any error occurs catch (Exception e) { // display the error System.out.println(e); } } } Output: URL: https://www.geeksforgeeks.org URI: https://www.geeksforgeeks.org Example 2: Java // Java program to convert URL to URI import java.net.*; class GFG { public static void main(String args[]) { // url and uri objects URL url = null; URI uri = null; try { // create an invalid URL url = new URL("https://www.geeksfor>geeks.com"); // display the URL System.out.println("URL: " + url); // convert the URL to URI uri = url.toURI(); // display the URI System.out.println("URI: " + uri); } // if any error occurs catch (Exception e) { // display the error 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 Comment More infoAdvertise with us Next Article URL toURI() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-net-package Java-URL Practice Tags : Java Similar Reads Path toUri() method in Java with Examples The toUri() method of java.nio.file.Path interface used to return a URI to represent this path. This method converts this path into an absolute URI with a scheme equal to the URI scheme that identifies the provider. The exact form of the scheme-specific part is highly provider dependent. In the scen 2 min read URL sameFile() method in Java with Examples The sameFile() function of Java.net.URL class is used to compare two URLs excluding the fragment part. This method returns true if both the URL are same excluding the fragment part else returns false. Function Signature public boolean sameFile(URL u) Syntax url1.sameFile(url2); Parameter: This metho 2 min read URL toExternalForm() method in Java with Examples The toExternalForm() function is a part of URL class. The function toExternalForm() returns the string representation of a specified URL. The string is created by calling the toExternalForm() function of the stream protocol handler for this object. Function Signature: public String toExternalForm() 2 min read URL getRef() method in Java with Examples The getRef() function is a part of URL class. The function getRef() returns the Reference or anchor part of a specified URL. Function Signature: public String getRef() Syntax: url.getRef() Parameter: This function does not require any parameter Return Type: The function returns String Type Below pro 2 min read Java File Class toURI() Method with Examples The toURI() method returns the URI that corresponds to the supplied URL. This method works similarly to the new URI (this.toString()). This abstract pathname is represented by a file: URI. Syntax: public URI toURI() Returns: It returns an absolute hierarchical URI with the scheme "file," a path that 1 min read Like