開博好久了,今天第一次發表技術文檔,之前總是將一些好的事例保存在電腦,時間久了找起來也很麻煩,所以還是放在博客里進行歸類比較方便,這樣也能將自己在學習過程中的一些心得體會分享給大家,也能給需要的人一點幫助。
一個朋友需要我幫忙給寫一個能夠提取網頁中Email地址的小程序,所以就用java語言幫他做了一個,有不完善的地方還請大家諒解,并提出來,一起學習。
源代碼詳見附件!加壓后將將readme.htm放在F://share//readme.htm,也可自定義目錄,自定義目錄需要修改對應的代碼文件路徑。源代碼.rar
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.regex.Matcher;import java.util.regex.Pattern;/*** email小爬蟲* @author xiaoxin* @date 2014/10/29*/public class EmailSpider { public static void main(String[] args) { try { BufferedReader br = new BufferedReader(new FileReader("F://share//readme.htm")); BufferedWriter bw = new BufferedWriter(new FileWriter("F://share//email.txt")); String line = ""; while((line = br.readLine()) != null) { parse(line, bw); } bw.flush(); bw.close(); br.close(); } catch (FileNotFoundException e) { e.PRintStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 解析Email的方法 * @param line 按行過濾 * @param bw 輸出到email.txt */ private static void parse(String line, BufferedWriter bw) { Pattern p = Pattern.compile("[//w[.-]]+@[//w[.-]]+//.[//w]+"); Matcher m = p.matcher(line); try { while(m.find()) { bw.write(m.group() + ";/r/n"); //換行顯示,適用于windows、linux下為/r、Mac下為/n // bw.newLine();//建議用這個換行 System.out.println(m.group()); } } catch (IOException e) { e.printStackTrace(); System.exit(-1); } }}新聞熱點
疑難解答