用URL的openStream()方法
比如
try{
URL u = new URL("http://www.sina.com.cn");
InputStream in = u.openStream();
Reader r = new InputStreamReader(new BufferedInputStream(in));
int c;
while((c = r.read()) != -1){
System.out.print((char)c)//或者System.out.write(c);
}
}
catch(MalformedURLException e){}
catch(IOException ignored){}
这就下载了sina的主页代码

解决方案 »

  1.   

    netdottrue(Acetrue)说的对!不过好像有点答非所问!star821116(足球和可口可乐是我的最爱) 说的就好多了!!
      

  2.   

    谢谢各位,我现在就想用java打开一个本地html文件,把他的源文件作为一个字符串返回.
    然后分析.
      

  3.   

    跟读文本文件一样,用File类就可以实现吧。
      

  4.   

    File htmlF = new File("filePath + fileName");
            BufferedReader bf = new BufferedReader(new FileReader(htmlF));
            String temp = bf.readLine();
            while (temp != null) {
               System.out.println(temp);
               temp = bf.readLine();
            }
      

  5.   

    star821116(足球和可口可乐是我的最爱)
    不是写得很对吗??
    有没有人试过?
    import java.net.*;
    import java.io.*;public class Gethtml {
        public static void main(String[] args) {
            try{
                URL u = new URL("http://www.sina.com.cn");
                InputStream in = u.openStream();
                Reader r = new InputStreamReader(new BufferedInputStream(in));
                int c;
                while((c = r.read()) != -1){
                System.out.print((char)c);//或者System.out.write(c);
                }
            }
            catch(MalformedURLException e){
                e.printStackTrace();}
            catch(IOException ignored){
                ignored.printStackTrace();
                }
        }
    }