how to extract only email address from a lines of text file(.txt or .doc) and print in another file?
January 1, 2010 at 9:10 AM
Hi,
Have a look at the below code and let me know if it helped you or if you have any further queries.
/**
* Program to scan email address from a file and write into an other file
*/
import java.io.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* @author helper
*
*/
public class EmailExtractor {
ArrayList<String> emailsList = new ArrayList<String>();
public static void main(String[] args) {
EmailExtractor ex = new EmailExtractor();
ex.fileScanner();
}
public void fileScanner() {
String fileName = "emailAddress";
String line = null;
String email="";
try {
File file = new File(fileName);
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
/*** Build a pattern of email address */
String regex ="^[\\w\\._-]+[@{1}]+[a-z+\\.{1,3}]+[a-z]$";
/***
* Explanation of RE: email address starts with alpha numeric characters,has '@'
* and has '.'at least once and to the max of three
* can also contain '_' '-'
* and ends with characters from a-z
* */
Pattern p = Pattern.compile(regex);
/*** Read each line of the line and break each line into tokens to see if it matches the pattern*/
while ((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()){
String token = st.nextToken();
Matcher m = p.matcher(token);
boolean result = m.matches();
/*** If a token matches the pattern, collect all the email address into an arrayList
* and use it later to write into an other file */
if(result){
email = m.group();
emailsList.add(email);
}
}
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
/*** write the email address to an other file. */
makeEmailList(emailsList);
}
public void makeEmailList(ArrayList emailArray){
String email="";
try{
FileWriter writer = new FileWriter("emailList.txt");
for(Iterator iter = emailArray.iterator(); iter.hasNext();){
email = (String) iter.next();
//System.out.println("email = "+email);
writer.write(email);
writer.write("\n");
}
writer.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}
------------------------------------------------------
a text file named: emailAddress.txt
Hi,
How are you doing?
I came to know you need help in writing a program.
I am woriking on it.
My email address is
helper.rose-india@gmail.com or
java_helper@yahoo.co.inIf any more queries, feel free to contact to my above email address.
Regards,
Helper.
-------------------------------------------------------
output in a file named emailList.txt
helper.rose-india@gmail.comjava_helper@yahoo.co.in
Related Tutorials/Questions & Answers:
Advertisements
email extractor - Java Beginnersemail extractor how to extract only
email address from a lines... queries.
/**
* Program to scan
email address from a file and write into an other...";
String line = null;
String
email="";
try {
File file = new File
email extractor - Java Beginnersemail extractor how to extract only
email address from a lines of text file(.txt or .doc) and print in another file?
will u send me answer in c language?
I have the idea ,copying one file text to another file by using buffer
emailemail how do i code for making clicking a send button sends a
email email email hi
I am using 1and1 server. Using this server i am sending a mail using java program .
But it is running some problem occur
" Cannot send
email. javax.mail.SendFailedException: Invalid Addresses;ADS_TO_REPLACE_1
E-Mail header extractorE-Mail header extractor i want to extract
email header with using of java so please show the code of
email extractor as result all the field show different color and or it extract ip where the
email came
E-Mail header extractorE-Mail header extractor i want to extract
email header with using of java so please show the code of
email extractor as result all the field show different color and or it extract ip where the
email came
ModuleNotFoundError: No module named 'Extractor'ModuleNotFoundError: No module named '
Extractor' Hi,
My Python... '
Extractor'
How to remove the ModuleNotFoundError: No module named '
Extractor' error?
Thanks
Hi,
In your python environment you
ModuleNotFoundError: No module named 'Extractor'ModuleNotFoundError: No module named '
Extractor' Hi,
My Python... '
Extractor'
How to remove the ModuleNotFoundError: No module named '
Extractor' error?
Thanks
Hi,
In your python environment you