其实很简单,一个正则表达式,搞定。。上代码:
package cn.abc.regex; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailExtract { public static void main(String[] args) throws Exception { Set<String> es=EmailUtils.EmailExtract(new File("E:\\javaStuday\\email.txt")); PrintWriter pw = new PrintWriter(new File("E:\\javaStuday\\email2.txt")); System.out.println(es.size()); for(String i:es) { pw.println(i); } pw.close(); } } class EmailUtils{ public static Set<String> EmailExtract(File f)throws Exception { Set<String> emails =new HashSet<String>(); BufferedReader buff = new BufferedReader(new FileReader(f)); String line = null; String reg = "\\w{5,}@\\w+(\\.[a-zA-Z]+)+"; Pattern pt = Pattern.compile(reg); while((line = buff.readLine())!=null) { Matcher m = pt.matcher(line); while(m.find()) { emails.add(m.group()); } } buff.close(); return emails; } }