URL

Convert between URL and URI

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: September 24th, 2013
0 5,423 1 minute read

With this example we are going to demonstrate how to convert between aURL and aURI. ClassURL 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. AURI 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 aURI by parsing a given string.
  • Construct a URL from this URI, usingtoURL() API method of URI.
  • Then create aURL object from a String representation.
  • Get theURI equivalent to this URL, usingtoURI() API method ofURL.

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 URItry {    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 URLtry {    url = uri.toURL();    System.out.println("URL from URI: " + url);}catch (MalformedURLException e) {System.out.println("Malformed URL: " + e.getMessage());}// Create a URLtry {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 URItry {    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.

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Byron KiourtzoglouByron KiourtzoglouNovember 11th, 2012Last Updated: September 24th, 2013
0 5,423 1 minute read
Photo of Byron Kiourtzoglou

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 atJava Code Geeks.

Related Articles

Bipartite Graph

Parse URL example

November 11th, 2012
Bipartite Graph

Read text from URL

November 11th, 2012
Bipartite Graph

java.net.URL Example

June 11th, 2014
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.