首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

Java 读取网页Html资料

2012-10-26 
Java 读取网页Html文件import java.net.*import java.io.*public class urlconnection{public static vo

Java 读取网页Html文件
import java.net.*;
import java.io.*;

public class urlconnection
{
    public static void main(String[] args)
    {
        StringBuffer document = new StringBuffer();
        try
        {
            URL url = new URL("http://www.163.com");
            URLConnection conn = url.openConnection();
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line = null;
            while ((line = reader.readLine()) != null)
            document.append(line + " ");
            reader.close();
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println(document.toString());
    }
}
==========================================================
方法二:
==========================================================
import java.net.*;
import java.io.*;

public class useSourceViewer
{
    public static void main (String[] args)
    {
        if (args.length > 0)
        {
            try
            {

                URL u = new URL(args[0]);
                InputStream in = u.openStream( );

                in = new BufferedInputStream(in);

                Reader r = new InputStreamReader(in);
                int c;
                while ((c = r.read( )) != -1)
                {
                    System.out.print((char) c);
                }
                Object o = u.getContent( );
                System.out.println("I got a " + o.getClass().getName( ));
            }
            catch (MalformedURLException e)
            {
                System.err.println(args[0] + " is not a parseable URL");
            }
            catch (IOException e)
            {
                System.err.println(e);
            }
        }
    }
}

热点排行