1 Packagedemo;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStream;6 ImportJava.io.InputStreamReader;7 8 /**9 * Web page content crawlingTen * @authorSy One * A */ - Public classgrabwebhtml{ - the Public Static voidMain (string[] args) { -String url= "Http://www.baidu.com"; - System.out.println (getwebhtml (URL)); - } + - Public Staticstring getwebhtml (string domain) { +StringBuffer SB =NewStringBuffer (); AInputStream is =NULL; atInputStreamReader ISR =NULL; -BufferedReader in =NULL; - Try { -Java.net.URL URL =NewJava.net.URL (domain); -is =Url.openstream (); -ISR =NewInputStreamReader (IS, "Utf-8"); inin =NewBufferedReader (ISR); - String Line; to while(line = In.readline ())! =NULL) { +Sb.append (line) append ("\ n"); - } the in.close (); * $}Catch(IOException e) {Panax Notoginseng e.printstacktrace (); -}finally { the Try { + if(in!=NULL){ A in.close (); thein=NULL; + } - if(isr!=NULL){ $ isr.close (); $Isr=NULL; - } - if(is!=NULL){ the is.close (); -is=NULL;Wuyi } the}Catch(IOException e) { - e.printstacktrace (); Wu } - } About returnsb.tostring (); $ } - -}
Java implements Web page content fetching