public String getDocumentAt(String urlString) {
StringBuffer document = new StringBuffer();
try {
URL url = new URL(urlString);
URLConnection conn = url
BufferedReader reader = new BufferedReader(new InputStreamReader(conn
String line = null;
while ((line = reader
document
reader
} catch (MalformedURLException e) {
System
} catch (IOException e) {
System
}
return document
}
getDocumentAt()方法有一個String類型的參數包含我們想得到的那份文檔的URL
URLConnection conn = url
一旦有了一個URLConnection
BufferedReader reader =new BufferedReader(new InputStreamReader(conn
有了BufferedReader
String line = null;
while ((line = reader
document
調用readLine()方法後從InputStream傳入行終止符(例如換行符)時才產生阻塞
當我們讀取所有行以後
reader
如果提供給urlString的URL構造器無效
總結
[
From:http://tw.wingwit.com/Article/program/Java/hx/201311/27251.html