Sunday 11 October 2015

Java URI class

URI stands for Uniform Resource Identifier. A uniform resource identifier (URI) is a string of characters used to identify a name of a resource.

URI’s are defined as two types URL and URN.

Uniform Resource Locator (URL): This is an address used to identify network/resource locations.
Example: http://gmail.com

Uniform Resource Name(URN): This is persistent name, which is address independent. A URN can be used to identify a resource without implying its location or how to access it.

Example: URN:ISBN:0-395-36341-1

Above one specifies the unique reference within the International Standard Book Number (ISBN) identifier system. It references a resource, but doesn’t specify how to obtain an actual copy of the book.

Usually, you should use the URL class when you want to download the content at a URL and the URI class when you want to use the URL for identification rather than retrieving the content.

Following is the general syntax for URI.
[scheme:]scheme-specific-part[#fragment]

URI class provides following constructors to initialize URI object.

URI(String str)
URI(String scheme, String ssp, String fragment)
URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment)
URI(String scheme, String host, String path, String fragment)
URI(String scheme, String authority, String path, String query, String fragment)

URI(String str)
Construts URI, by parsing given string. If the string passed to this constructor, is not followed URI standrads, then it throws URISyntaxException.

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String args[]) throws URISyntaxException {
    URI uri1 = new URI("http://www.google.com");
    URI uri2 = new URI("URN:ISBN:0-395-36341-1");

    System.out.println(uri1);
    System.out.println("Authority : " + uri1.getAuthority());
    System.out.println("Fragment : " + uri1.getFragment());
    System.out.println("Host : " + uri1.getHost());
    System.out.println("Scheme : " + uri1.getScheme());
    System.out.println("********************************");

    System.out.println(uri2);
    System.out.println("Authority : " + uri2.getAuthority());
    System.out.println("Fragment : " + uri2.getFragment());
    System.out.println("Host : " + uri2.getHost());
    System.out.println("Scheme : " + uri2.getScheme());
  }
}


Output

http://www.google.com
Authority : www.google.com
Fragment : null
Host : www.google.com
Scheme : http
********************************
URN:ISBN:0-395-36341-1
Authority : null
Fragment : null
Host : null
Scheme : URN
null


URI(String scheme, String ssp, String fragment)
Constructs URI from the components scheme (http, ftp, smtp etc., ), ssp (Scheme Specific part), fragment. Final result like “scheme:ssp#fragment”.

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String args[]) throws URISyntaxException {
    URI uri1 = new URI("http", "//www.google.com", "search1");

    System.out.println(uri1);
    System.out.println("Authority : " + uri1.getAuthority());
    System.out.println("Fragment : " + uri1.getFragment());
    System.out.println("Host : " + uri1.getHost());
    System.out.println("Scheme : " + uri1.getScheme());
  }
}


Output

http://www.google.com#search1
Authority : www.google.com
Fragment : search1
Host : www.google.com
Scheme : http


public URI(String scheme, String host, String path, String fragment) throws URISyntaxException
Constructs URI from the components scheme (http, ftp, smtp etc., ), host (host name), path, fragment. Final result like “scheme:host/path#fragment”.

import java.net.URI;
import java.net.URISyntaxException;

public class Main {
  public static void main(String args[]) throws URISyntaxException {
    URI uri = new URI("http", "www.docs.oracle.com", "/javase/7/docs/api/java/net/URI.html", "URI");
    
    System.out.println(uri);
    System.out.println("Authority : " + uri.getAuthority());
    System.out.println("Fragment : " + uri.getFragment());
    System.out.println("Host : " + uri.getHost());
    System.out.println("Scheme : " + uri.getScheme());
  }
}


Output
http://www.docs.oracle.com/javase/7/docs/api/java/net/URI.html#    




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment