d:/merge.html文件 自选,选取一个网站下载到本地目录下,也可以采用以下文件为txt测试使用
waneasu@263.com 2444169361@qq.com 2446565869@163.com一种较为标准的测试网址中邮箱数量的代码
package work; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class test { public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br=null; try { Pattern pattern = null; Matcher matcher = null; br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/text/number.html"))); pattern=Pattern.compile("^[a-zA-Z0-9]+@[a-zA-Z0-9]+(\\.[a-zA-Z0-9]+)+$"); int i = 0; String line=null; System.out.println("开始读取"); while ((line = br.readLine()) != null) { matcher=pattern.matcher(line); while(matcher.find()) { i++; } } System.out.println(i); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }