第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());
}