服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

利用io流:::(二种方法)读取网页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);
}
}
}
}



===========================================
提示:
===========================================
io流的应用很灵活,大家还可以用别的方法读取!
大家可以比较以上2种方法的优劣!
==========================================

扫描关注微信公众号