爲了寫一個java的採集程序,從網上學習到3種方法能夠獲取單個網頁內容的方法,主要是運用到是java IO流方面的知識,對其不熟悉,所以寫個小結。php
import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Get_Html { public static void main(String[] args) throws Exception { long start= System.currentTimeMillis(); String str_url="http://www.hiphop8.com/city/guangdong/guangzhou.php"; Pattern p = Pattern.compile(">(13\\d{5}|15\\d{5}|18\\d{5}|147\\d{4})<"); //String html = get_Html_2(str_url); //String html = get_Html_1(str_url); String html = get_Html_3(str_url); Matcher m = p.matcher(html); int num = 0; while(m.find()) { System.out.println("打印出的號碼段落:"+m.group(1)+" 編號"+(++num)); } System.out.println(num); long end = System.currentTimeMillis(); System.out.println("花費的時間"+(end-start)+"毫秒"); } public static String get_Html_2(String str_url) throws IOException{ URL url = new URL(str_url); String content=""; StringBuffer page = new StringBuffer(); try { BufferedReader in = new BufferedReader(new InputStreamReader(url .openStream(), "utf-8")); while((content = in.readLine()) != null){ page.append(content); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return page.toString(); } public static String get_Html_1(String str_url) throws IOException{ URL url = new URL(str_url); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); InputStreamReader input = new InputStreamReader(conn.getInputStream(), "utf-8"); BufferedReader bufReader = new BufferedReader(input); String line = ""; StringBuilder contentBuf = new StringBuilder(); while ((line = bufReader.readLine()) != null) { contentBuf.append(line); } return contentBuf.toString(); } /** * 經過網站域名URL獲取該網站的源碼 * @param url * @return String * @throws Exception */ public static String get_Html_3(String str_url) throws Exception { URL url = new URL(str_url); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); //設置鏈接超時 java.io.InputStream inStream = conn.getInputStream(); //經過輸入流獲取html二進制數據 byte[] data = readInputStream(inStream); //把二進制數據轉化爲byte字節數據 String htmlSource = new String(data); return htmlSource; } /** * 把二進制流轉化爲byte字節數組 * @param inStream * @return byte[] * @throws Exception */ public static byte[] readInputStream(java.io.InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1204]; int len = 0; while ((len = inStream.read(buffer)) != -1){ outStream.write(buffer,0,len); } inStream.close(); return outStream.toByteArray(); } }
【分別測試6次的結果】不知道是否是獲取的網頁數量內容較小,採集效率差很少,不過方法2應該是最好最簡便的。html
//get_Html_1 967 2658 1132 1199 988 1236
//get_Html_2 2323 2244 1202 1166 1081 1011
//get_Html_3 978 1219 1527 1133 1192 1774java
兩者返回的的都是InputStrema對象,且都是經過openConnection()方法獲取URLConnection對象,而後調用getInputStream()方法,因此方法2和方法1是同樣的,但前者更方便。數組
【該類的功能】:能將 字符流 放入緩衝區(內存中的一塊小區域),以便實現高效的讀取。app
【看構造方法】:函數
BufferedReader(Reader in) 建立一個使用默認大小輸入緩衝區來緩衝字符輸入流。學習
BufferedReader(Reader in, int sz) 建立一個使用指定大小輸入緩衝區的緩衝字符輸入流。測試
【經常使用方法】:readLine()能夠快速的實現文本字符的行讀取。網站
InputStreamReader 是從字節流到字符流的橋樑:它讀入字節,並根據指定的編碼方式,將之轉換爲字符流,它是Reader的子類。ui
而爲了達到更高效率,咱們常常用 BufferedReader 封裝 InputStreamReader , 因此咱們常常看到的用法是
這裏的InputStreamReader類的功能是將字節流轉換爲字符流,因此以上語句實現了 :將 字節輸入流 轉換爲 字符輸入流 且放置緩衝區。
它是OutputStream類的擴展類,其構造函數是byteArrayInputStream(byte []buf),做用是把字節數組buf 變成輸入流的形式,並經過toString()或者toByteArray()方法或得想要的數據形式。方法3中的readInputStream方法可改成返回String類型,將後面的outStream.toByteArray()改成outStream.toString()方法,這樣又精簡了代碼。
InputStream與OutputStream: 是 8位字節 輸入/輸出流類的基類,主要用在處理二進制數據,它是按字節來處理的。文件在硬盤或在傳輸時都是以字節的方式進行的,包括圖片等都是按字節的方式存儲的,其他的字節流的處理類都是對該類的擴展,如等上面講ByteArrayInputStream類。
因爲InputStream.read()方法是每次從流裏只讀取讀取一個字節,效率會很是低。而InputStream.read(byte[] b)或者InputStream.read(byte[] b,int off,int len)方法,一次能夠讀取多個字節,效率較高,因此方法3中建立了一個byte字節數組,以便一次性讀取更多的字節。當read()方法讀取內容爲空的時候,返回-1.
另外字符輸入輸出流的基類 Reader/Writer,且要知道1個字符= 2字節,字符都是在內存中生成的,一箇中文佔兩個字節,其子類包含有上面講的的InputStreamRead類與BufferReader類。
寫了幾點總結,都是和java的IO流有關的,是否是應該改個標題,想一想仍是算了,畢竟採集程序中很重要的一部分就是IO流方面的,java在IO流方面提供了豐富的類庫,邊學邊積累吧。