从网页源文件中得到链接 |
|
www.nanhushi.com 佚名 不详 |
这是一个从网页源文件中得到链接的实现代码,和大家分享一下:
import Java.net.*; import Java.io.*; import Java.util.*;
class ScanPage{ private static String strPage; private String strUrl; private String fileName; public void setURL(String strUrl){ this.strUrl=strUrl; } //从地址中得到文件名 public void setFileName(){ int i; char ch; i=strUrl.length(); ch=strUrl.charAt(--i); while(ch!=’/’ && ch>0) ch=strUrl.charAt(--i); fileName=strUrl.substring(i); } //下载网页 public void downFile()throws IOException{ URL url =new URL(strUrl); InputStream is =url.openStream(); OutputStream os =new FileOutputStream(fileName); byte[] buffer =new byte[512]; int len; while((len =is.read(buffer))!=-1) os.write(buffer,0,len); is.close(); os.close(); } //读文件 public void readFile() throws IOException { StringBuffer sb = new StringBuffer(); BufferedReader in =new BufferedReader(new FileReader(fileName)); String s;
while((s = in.readLine()) != null) { sb.append(s); sb.append("\n"); } in.close(); strPage=sb.toString(); } public String getTitle(){ return ""; }
|
|
|
文章录入:杜斌 责任编辑:杜斌 |
|
上一篇文章: Java打包实现方法详解 下一篇文章: 使用用内存流读写文件的代码 |
【字体:小 大】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口】 |
|
|