WEB开发网
开发学院WEB开发Jsp 利用IO流:::(二种方法)读取网页HTML网页代码... 阅读

利用IO流:::(二种方法)读取网页HTML网页代码!

 2008-01-05 08:51:16 来源:WEB开发网   
核心提示:== 方法 一:==import java.net.*;import java.io.*;public class urlconnection{ public static void main(String[] args) { StringBuffer document = new Strin

  ======================================================================
方法 一:
======================================================================
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种方法的优劣!
==========================================

Tags:利用 IO 方法

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接