java 邮件无法解析html_Java读取html页面文件解析邮箱地址

该Java代码示例展示了如何从HTML文件或网络URL中读取内容,并使用正则表达式解析电子邮件地址。通过检查字符串中是否存在@符号,然后提取和清理包含邮箱地址的片段,最终将结果写入文件。
摘要由CSDN通过智能技术生成

[java]代码库package com.alpha.test;import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.Reader;

import java.io.Writer;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import java.util.regex.Matcher;

import java.util.regex.Pattern;/**

* 读取html页面文件解析邮箱地址

*

* @author JavaAlpha 2012-12-19 13:45:11

*/

public class ReadHtmlToTxt { // 读取文件

public static String readHtml(String path) { StringBuffer emailCont = new StringBuffer(); File htmlFile = new File(path);

if (htmlFile.exists() && htmlFile.isFile() && htmlFile.canRead()) {

Reader in;

try {

in = new FileReader(htmlFile);

char[] buff = new char[4096];

int nch;

while ((nch = in.read(buff, 0, buff.length)) != -1) {

emailCont.append(checkEmail(new String(buff, 0, nch)));

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} } return emailCont.toString();

} // 判断字符串里面是否包括@符号

public static String checkEmail(String str) { String postCont = "";

// 判断是否回复的内容

if (str.indexOf("@") > -1) { postCont = str.substring(str.indexOf("@") - 10,

str.indexOf("@") + 10); if (postCont.indexOf(">") > -1 || postCont.indexOf(" -1) {

postCont = postCont.replaceAll(">", "");

postCont = postCont.replaceAll("

postCont = postCont.replaceAll("/", "");

} if (postCont.indexOf(",") > -1 || postCont.indexOf(",") > -1

|| postCont.indexOf("。") > -1 || postCont.indexOf(";") > -1) {

postCont = postCont.replaceAll(",", "");

postCont = postCont.replaceAll(",", "");

postCont = postCont.replaceAll("。", "");

} postCont = postCont.substring(0, postCont.indexOf(".com") + 4); System.out.println(postCont);

} return postCont;

}

//过滤汉字

public static boolean checkChinese(String str) {

String regEx = "[\\u4e00-\\u9fa5]";

Pattern p = Pattern.compile(regEx);

Matcher m = p.matcher(str);

if (m != null && m.find()){

return true;//是汉字

}

return false;

} // 将整理是邮箱地址写入文件

public static void writerFile(String cont, String path) { File emailFile = new File(path); try {

//如果文件不存在,创建文件

if (!emailFile.exists()) {

emailFile.createNewFile();

}

Writer out = new FileWriter(emailFile); out.write(cont);

out.flush();

out.close();

} catch (Exception e) {

e.printStackTrace();

} }

/**

* 读取网络内容

*/

public static void readUrlCont(String strUrl) {

StringBuffer cont = new StringBuffer();//内容

try {

URL url = new URL(strUrl);

URLConnection conn = url.openConnection();

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String lineCont = "";

while ((lineCont = reader.readLine())!= null) {

cont.append(lineCont+"");

}

reader.close();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println(cont.toString());

} public static void main(String[] args) {

//String cont = readHtml("e://test.htm");//读取文件

//writerFile(cont, "e://test.txt");//写文件

//checkChinese("qwe123");

readUrlCont("http://www.163.com");

}}

//源代码片段来自云代码http://yuncode.net

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值