Java URL toURI() method
The toURI() method returns URI equivalent to given URL. This method functions in the same way as new URI (this.toString()).
A URI instance is equivalent to this URL.
URISyntaxException - if this URL is not formatted strictly according to RFC2396 and cannot be converted to a URI.
Example 1Test it Now
URL created: https://www.javatpoint.com/URL-class URI from URL: https://www.javatpoint.com/URL-class
URL:file:/D:/Advancejava/UrlClass/file%20name%20with%20spaces.txt URI:file:/D:/Advancejava/UrlClass/file%20name%20with%20spaces.txt URL (no special character handling):file:/D:/Advancejava/UrlClass/file name with spaces.txt