ZhangLiHai.Com Blog


建立URL读取数据到控制台上

张利海 于 2003年05月23日 发表

这是以前学习时的一个例子:


import java.net.*;
import java.io.*;
/**
URL读取页面内容
FileName:SocketTest.java
@Author:Alec Cheung
MSN:eboysit@hotmail.com
Home:www.andyfans.com
http://97741.126.com
http://cheung.cxc.cc
Date:2002-11-7
*/

//:~
public class URLSourceViewer
{
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);
StringBuffer buf = new StringBuffer();
int c;
while((c=r.read()) != -1)
{
buf.append((char) c);
} ///end while
System.out.println("==============="+args[0]+" Content================");
System.out.println(buf.toString().trim());
}//end try
catch(MalformedURLException e){
System.err.println(args[0]+" is not a parseInt URL.");
}
catch(IOException e){
System.err.println(e);
}
} //end if
} //end main
} //:end URLSourceViewer

新版本Blog中有更多内容
Copyright (C)2002-2007 All Rights Reserved Powered By:ZhangLiHai.Com