java怎么通过正则表达式提取一个文件里面的所有邮箱?

如题所述

package org.com.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class ReadTxt {

static int NUM = 231;
static String[] value = new String[NUM];

public static List<String> listFriends(File file) throws InterruptedException {
List<String> listFriends = new ArrayList<String>();
int n =0;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = null;
while ((line = reader.readLine()) != null) {
for (int i = 0; i < NUM; i++) {
int beginIndex = line.indexOf(" n=");
int endIndex = line.indexOf(".com");
if(beginIndex>endIndex){
System.out.println("you are wrong!!!!!!");
n=n+1;
// Thread.sleep(3000);
break;
}
if(beginIndex>-1&&endIndex>-1){
value[i] = line.substring(beginIndex, endIndex);
value[i] = value[i].replaceAll("n=", "<!--");
value[i] = value[i]
.replaceAll("e=", "--><email><receiver>");
value[i] = value[i].replaceAll("\"", "");
listFriends.add(value[i] + "@qq.com</receiver></email>");
// line = line.substring(endIndex * 2 - beginIndex + 2);
break;
}
else {
System.out.println("please go on!!!!!!");
// Thread.sleep(3000);
break;
}
}
}

reader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listFriends = removeDuplicateObj(listFriends);
System.out.println(n);
return listFriends;

}

public static List<String> removeDuplicateObj(List<String> list) {
Set<String> someSet = new LinkedHashSet<String>(list);
Iterator<String> iterator = someSet.iterator();
List<String> tempList = new ArrayList<String>();
int i = 0;
while (iterator.hasNext()) {
tempList.add(iterator.next().toString());
i++;
}
return tempList;
}

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
File file = new File(
"C:\\Documents and Settings\\Administrator\\桌面\\tttttttttttttttttt.txt");
List<String> listFriends = new ArrayList<String>();
listFriends = listFriends(file);
for (String str : listFriends) {
System.out.println(str);
}
System.out.println(listFriends.size());
}

}
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2017-09-19
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("H:\\emailtest.txt"));
try {
while((bufferedReader.readLine())!=null)
{
//System.out.println(line);
Parseemail(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void Parseemail(String line)
{
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter("H:\\reslut.java"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Pattern pattern = Pattern.compile("[\\w[.-]]+\\@[\\w[.-]]{2,}\\.[\\w[.-]]+");
Matcher matcher = pattern.matcher(line);
while(matcher.find())
{
System.out.println(matcher.group());
String string = new String(matcher.group());
string += "\n";
try {
bufferedWriter.newLine();
bufferedWriter.write(string, 0, string.length());
bufferedWriter.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
bufferedWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

本回答被提问者采纳
第2个回答  2016-01-19
文件内容读成一个字符串

Pattern p=Pattern.compile("[\w]+@[\w]+\.[com|net|cn]");
Matcher m=p.matcher(文件内容);
while(m.find()){
vfp = m.group();
System.out.println(vfp)://打印所有邮箱

}
第3个回答  2015-11-08
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;

public class EmailParser {

private final static Pattern emailer = Pattern
.compile("[\\w[.-]]+@[\\w[.-]]+\\.[\\w]+");
/**
 * @param args
 * @throws IOException
 */
public static void main(String[] args) throws IOException {
String txt = FileUtils.readFileToString(new File("你的文件"));
Matcher matchr = emailer.matcher(txt);
while (matchr.find()) {
String email = matchr.group();
System.out.println(email);
}
}

}

第4个回答  2013-05-08
Pattern p = Pattern.compile("[_/.0-9a-z-]+@([0-9a-z][0-9a-z-]+/.)+[a-z]{2,3}");
Matcher m = p.matcher(builderStr);
while (m.find()) {
System.out.println(m.group());
}