URI Relativization

In this chapter you will learn:

  1. How to do URI Relativization

Relativization

Relativization is the inverse of resolution.

URI declares a URI relativize(URI uri) method for relativizing its uri argument against the URI in the current URI object.

For any two normalized URI instances u and v, u.relativize(u.resolve(v)).equals(v) and u.resolve(u.relativize(v)).equals(v) evaluate to true.

relativize() performs relativization of its URI argument's URI against the URI in the URI object on which this method was called as follows:

import java.net.URI;
import java.net.URISyntaxException;
/*  j av a2  s  . c  om*/
public class Main {
  public static void main(String[] args) throws URISyntaxException {
    URI uri1 = new URI("http://java2s.com/");
    URI uri2 = new URI("http://java2s.com/index.htm");
    System.out.println("Relativized URI = " + uri1.relativize(uri2));
  }
}

Output:

Next chapter...

What you will learn in the next chapter:

  1. How to convert URI to URL with toURL()
Home » Java Tutorial » URL URI
URL
URL Creation
URL for jar file
URL Components
Convert file path to URL
URL Relative
URL Protocol
Read from URL
Compare URL
URLConnection
HTTP Header
URLConnection Post
Cookie
URLConnection Read
HttpURLConnection
HttpURLConnection Properties
HttpURLConnection proxy
HttpURLConnection Authenticator
HTTPS
JarURLConnection
Encode a URL
Decode a URL
URI
URI Normalization
URI Resolution
URI Relativization
Convert URI to URL
IP Address
IP Ping
NetworkInterface