Java编程之URI

一旦拥有了URI对象,你就可以通过调用getAuthority()、getFragment()、getHost()、getPath()、getPort()、getQuery()、getScheme()、getSchemeSpecificPart()和 getUserInfo()方法提取多种组件。你也可以通过调用isAbsolute()确定该URI是绝对的还是相对的,通过调用isOpaque()确定该URI是不透明的还是分层的。如果返回值是true意味着该URI是绝对的或不透明的,如果返回值是false意味着该URI是相对的或分层的。

  列表1中的程序用命令行参数建立了一个URI对象,调用URI组件提取方法来检索URI的组件,并调用URI的isAbsolute()和isOpaque()方法把该URI分类为绝对的/相对性和不透明的/分层的。

  列表1: URIDemo1.java

  
    
1 // URIDemo1.java
2
3 import java.net. * ;
4
5 class URIDemo1
6 {
7 public static void main (String [] args) throws Exception
8 {
9 if (args.length != 1 )
10 {
11 System.err.println ( " usage: java URIDemo1 uri " );
12 return ;
13 }
14
15 URI uri = new URI (args [ 0 ]);
16
17 System.out.println ( " Authority = " + uri.getAuthority ());
18 System.out.println ( " Fragment = " + uri.getFragment ());
19 System.out.println ( " Host = " + uri.getHost ());
20 System.out.println ( " Path = " + uri.getPath ());
21 System.out.println ( " Port = " + uri.getPort ());
22 System.out.println ( " Query = " + uri.getQuery ());
23 System.out.println ( " Scheme = " + uri.getScheme ());
24 System.out.println ( " Scheme-specific part = " +
25 uri.getSchemeSpecificPart ());
26 System.out.println ( " User Info = " + uri.getUserInfo ());
27 System.out.println ( " URI is absolute: " + uri.isAbsolute ());
28 System.out.println ( " URI is opaque: " + uri.isOpaque ());
29 }
30 }

  输入java URIDemo1命令后,列表1的输出结果如下:

  
    
1 query: // [email protected]:9000/public/manuals/appliances?stove#ge:
2 Authority = [email protected]: 9000
3 Fragment = ge
4 Host = books.com
5 Path = / public / manuals / appliances
6 Port = 9000
7 Query = stove
8 Scheme = query
9 // [email protected]:9000/public/manuals/appliances?stove
10 User Info = jeff
11 URI is absolute: true
12 URI is opaque: false

  上面的输出显示该URI是绝对的,因为它指定了一个大纲(query),并且该URI是分层的,因为query后面有/符号。

   技巧

  你应该调用URI的compareTo(Object o)和equals(Object o)来决定URI的次序(为了排序目的)和等同性。你可以参考SDK文档,查阅这些方法的更多信息。

你可能感兴趣的:(java编程)