URL對象的創建及使用 URL類中一些很基本的方法
URL類中一些很基本的方法如下:
·
public final Obect getContent() 這個方法取得傳輸協議。
·
public String getFile() 這個方法取得資源的文件名。
·
public String getHost() 這個方法取得機器的名稱。
·
public int getPort() 這個方法取得端口號。
·
public String getProtocol() 這個方法取得傳輸協議。
·
public String toString() 這個方法把URL轉化為字符串。
實例:URL對象的創建及使用 }import java.net. *;
import java.io.*;
class Myurl {
public static void main(String args[]) {
try {
URL url=new URL("http://www.tsinghua.edu.cn/chn/index.htm");
System.out.println("the Protocol: "+url.getProtocol());
System.out.println("the hostname: " +url.getHost());
System.out.println("the port: "+url.getPort());
System.out.println("the file:"+url.getFile());
System.out.println(url.toString());
}catch(MalformedURLException e) {
System.out.println(e);
}
}
posted on 2011-04-21 19:50 haojinlian 閱讀(1070) 評論(0) 編輯 收藏