Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 399
نمايش فراداده

URIjava.net

Java 1.4serializable comparable

The URI class is an immutable representation of a Uniform Resource Identifier or URI. A URI is a generalization of the URLs or Uniform Resource Locators used on the world wide web. The URI supports parsing and textual manipulation of URI strings, but does not have any direct networking capabilities the way that the URL class does. The advantages of the URI class over the URL class are that it provides more general facilities for parsing and manipulating URLs than the URL class, that it can can represent relative URIs which do not include a scheme (or protocol), and that it can manipulate URIs that include unsupported or even unknown schemes.

Obtain a URI with one of the constructors, which allow a URI to be parsed from a single string, or allow the specification of the individual components of a URI. These constructors can throw URISyntaxException, which is a checked exception. When using hard-coded URIs (rather than URIs based on user input) you may prefer to use the static create( ) method which does not throw any checked exceptions.

Once you have created a URI, object you can use the various get methods to query the various portions of the URI. The getraw( ) methods are like the get( ) methods except that they do not decode hexadecimal escape sequences of the form %xx that appear in the URI. normalize( ) returns a new URI object that has "." and unnecessary ".." sequences removed from its path component. resolve( ) interprets its URI (or string) argument relative to this URI and returns the result. relativize( ) performs the reverse operation. It returns a new URI which represents the same resource as the specified URI argument, but which is relative to this URI. Finally, the toURL( ) method converts an absolute URI object to the equivalent URL. Since the URI class provides superior textual manipulation capabilities for URLs, it can be useful to use the URI class to resolve relative URLs (for example) and then convert those URI objects to URL objects when they are ready for networking.

Figure 12-25. java.net.URI

public final class

URI implements Comparable<URI>, Serializable { // Public Constructors public

URI (String

str ) throws URISyntaxException; public

URI (String

scheme , String

ssp , String

fragment ) throws URISyntaxException; public

URI (String

scheme , String

host , String

path , String

fragment ) throws URISyntaxException; public

URI (String

scheme , String

authority , String

path , String

query , String

fragment ) throws URISyntaxException; public

URI (String

scheme , String

userInfo , String

host , int

port , String

path , String

query , String

fragment ) throws URISyntaxException; // Public Class Methods public static URI

create (String

str ); // Public Instance Methods public String

getAuthority ( ); public String

getFragment ( ); public String

getHost ( ); public String

getPath ( ); public int

getPort ( ); public String

getQuery ( ); public String

getRawAuthority ( ); public String

getRawFragment ( ); public String

getRawPath ( ); public String

getRawQuery ( ); public String

getRawSchemeSpecificPart ( ); public String

getRawUserInfo ( ); public String

getScheme ( ); public String

getSchemeSpecificPart ( ); public String

getUserInfo ( ); public boolean

isAbsolute ( ); public boolean

isOpaque ( ); public URI

normalize ( ); public URI

parseServerAuthority ( ) throws URISyntaxException; public URI

relativize (URI

uri ); public URI

resolve (URI

uri ); public URI

resolve (String

str ); public String

toASCIIString ( ); public URL

toURL ( ) throws MalformedURLException; // Methods Implementing Comparable

5.0 public int

compareTo (URI

that ); // Public Methods Overriding Object public boolean

equals (Object

ob ); public int

hashCode ( ); public String

toString ( ); }

Passed To

java.io.File.File( ), CookieHandler.{get( ), put( )}, ProxySelector.{connectFailed( ), select( )}, ResponseCache.{get( ), put( )}

Returned By

java.io.File.toURI( ), URL.toURI( )