Java Reference
In-Depth Information
URI
styles
=
new
URI
(
"ftp"
,
"anonymous:elharo@ibiblio.org"
,
"ftp.oreilly.com"
,
21
,
"/pub/stylesheet"
,
null
,
null
);
However, the resulting URI still has to follow all the usual rules for URIs; and again null
can be passed for any argument to omit it from the result.
If you're sure your URIs are legal and do not violate any of the rules, you can use the
static factory
URI.create()
method instead. Unlike the constructors, it does not throw
a
URISyntaxException
. For example, this invocation creates a
URI
for anonymous FTP
access using an email address as password:
URI
styles
=
URI
.
create
(
"ftp://anonymous:elharo%40ibiblio.org@ftp.oreilly.com:21/pub/stylesheet"
);
If the URI does prove to be malformed, then an
IllegalArgumentException
is thrown
by this method. This is a runtime exception, so you don't have to explicitly declare it or
catch it.
The Parts of the URI
A URI reference has up to three parts: a scheme, a scheme-specific part, and a fragment
identifier. The general format is:
scheme
:
scheme-specific-part
:
fragment
If the scheme is omitted, the URI reference is relative. If the fragment identifier is omit‐
ted, the URI reference is a pure URI. The
URI
class has getter methods that return these
three parts of each
URI
object. The
getRawFoo()
methods return the encoded forms of
the parts of the URI, while the equivalent
getFoo()
methods first decode any percent-
escaped characters and then return the decoded part:
public
String
getScheme
()
public
String
getSchemeSpecificPart
()
public
String
getRawSchemeSpecificPart
()
public
String
getFragment
()
public
String
getRawFragment
()
There's no
getRawScheme()
method because the URI specification re‐
quires that all scheme names be composed exclusively of URI-legal
ASCII characters and does not allow percent escapes in scheme names.
These methods all return null if the particular
URI
object does not have the relevant
component: for example, a relative URI without a scheme or an http URI without a
fragment identifier.
A URI that has a scheme is an
absolute
URI. A URI without a scheme is
relative
. The
isAbsolute()
method returns true if the URI is absolute, false if it's relative: