Converting from a URL to a URI


The URI class  represents a Unified Recourse Indicator. The URL represents a Unified Resource Locator.

What’s the difference?

URI can be further classified as a locator, a name, or both. The term “Uniform Resource Locator” (URL) refers to the subset of URIs that, in addition to identifying a resource, provide a means of locating the resource by describing its primary access mechanism (e.g., its network “location”). In other words; the URI is more specific than the URL. However. The W3C realized that there is a ton of confusion about this. They issued a URI clarification document that says that it is now OK to use the terms URL and URI interchangeably (to mean URI). It is no longer useful to strictly segment URIs into different types, which means that according to W3C there is no longer a difference between the two. However they have different Classes. The example below shows how to convert between the two.

URI uri = null;
URL url = null;

// Create a URI
try {
    uri = new URI("file://index.html");
} catch (URISyntaxException e) {

// Convert an absolute URI to a URL
try {
    url = uri.toURL();
} catch (IllegalArgumentException e) {
    // URI was not absolute
} catch (MalformedURLException e) {

// Convert a URL to a URI
try {
    uri = new URI(url.toString());
} catch (URISyntaxException e) {


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.