URL

Convert between URL and URI

With this example we are going to demonstrate how to convert between a URL and a URI. Class URL represents a Uniform Resource Locator, a pointer to a “resource” on the World Wide Web. A resource can be something as simple as a file or a directory, or it can be a reference to a more complicated object, such as a query to a database or to a search engine. A URI represents a Uniform Resource Identifier (URI) reference. The URI class provides constructors for creating URI instances from their components or by parsing their string forms, methods for accessing the various components of an instance, and methods for normalizing, resolving, and relativizing URI instances. Instances of this class are immutable. In short, to convert between a URL and a URI you should:

  • Construct a URI by parsing a given string.
  • Construct a URL from this URI, using toURL() API method of URI.
  • Then create a URL object from a String representation.
  • Get the URI equivalent to this URL, using toURI() API method of URL.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class ConvertBetweenURLAndURI {
	
	public static void main(String[] args) {
		
		URI uri = null;
		URL url = null;

		// Create a URI
		try {
		    uri = new URI("http://www.javacodegeeks.com/");
		    System.out.println("URI created: " + uri);
		}
		catch (URISyntaxException e) {
			System.out.println("URI Syntax Error: " + e.getMessage());
		}

		// Convert URI to URL
		try {
		    url = uri.toURL();
		    System.out.println("URL from URI: " + url);
		}
		catch (MalformedURLException e) {
			System.out.println("Malformed URL: " + e.getMessage());
		}
		
		// Create a URL
		try {
			url = new URL("http://examples.javacodegeeks.com/");
		    System.out.println("URL created: " + url);
		}
		catch (MalformedURLException e) {
			System.out.println("Malformed URL: " + e.getMessage());
		}

		// Convert a URL to a URI
		try {
		    uri = url.toURI();
		    System.out.println("URI from URL: " + uri);
		}
		catch (URISyntaxException e) {
			System.out.println("URI Syntax Error: " + e.getMessage());
		}
		
	}

}

Output:

URI created: http://www.javacodegeeks.com/
URL from URI: http://www.javacodegeeks.com/
URL created: http://examples.javacodegeeks.com/
URI from URL: http://examples.javacodegeeks.com/

  
This was an example of how to convert between a URL and a URI in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button