program to read/find emails from the given text file
Today morning i need to collect some emails of java guys from the java fourms or websites. So i started copying emails in this page by slecting the email and copying using Ctrl+C, i have done 20 emails copied. Then i lose my patience to copy. Immediately i got an idea, why can't i write a program to read all the emails from the page. Then i copied all the text from the page and pasted in a text file named as
All the emails are unique so we need a collection to store. So lets take Set or HashSet and read the file using
Go to command prompt to compile our java packaged program
then run the EmailReader program
The emails file named ouput is generated with all emails.
Reference Books:
emails.txt
. Now the java program came in to the picture and we should use java.io
package to write or read file operations. In addition i am taking java.util.HashSet
and java.util.regex
packages to start our program. Here we are reading a text file containing emails with text and writing into another text file this is called deserialization and serialization.All the emails are unique so we need a collection to store. So lets take Set or HashSet and read the file using
java.io.FileReader
FileReader file = new FileReader(new File("emails.txt"));Read line by line from the text file using StringBuffer and validate each line is there any email is placed in between the text for words. For this validation we are using Regex(Regular Expressions). If any email found then keep it in HashSet container. We are using a method named
fillEmailsHashSet(line, hs);
with parameters as String 'line' and 'h's is the object of HashSet.BufferedReader br = new BufferedReader(file); String line; try { while ((line = br.readLine()) != null) { fillEmailsHashSet(line, hs); } }fillEmailsHashSet is defined as below with the Regex validation. If the line is containing an email format then we add it to the HashSet.
public static void fillEmailsHashSet(String line,HashSet<String> container){ Pattern p = Pattern.compile("([\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[A-Za-z]{2,4})"); Matcher m = p.matcher(line); while(m.find()) { container.add(m.group(1)); } }after finding emails from the text, write those to a new file named output.txt is also called as Serialization.
BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter("./output.txt")); for (String string : hs) { writer.write(string); writer.newLine(); }Now let's combine all the code snippets and run our program to read or find emails from the given text file.
package com.javabynataraj; //http://javabynataraj.blogspot.com import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashSet; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailReader { public static void main(String[] args) { HashSet<String> hs = new HashSet<String>(); FileReader file = null; try { file = new FileReader(new File("emails.txt")); } catch (FileNotFoundException e1) { System.err.println("File emails.txt not found!"); e1.printStackTrace(); } BufferedReader br = new BufferedReader(file); String line; try { while ((line = br.readLine()) != null) { fillEmailsHashSet(line, hs); } } catch (IOException e) { System.err.println("Error when reading"); e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { System.err.println("Unexpected error"); e.printStackTrace(); } } } // write the emails to a text file BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter("./output.txt")); for (String string : hs) { writer.write(string); writer.newLine(); } } catch (IOException e) { System.err.println(e); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { System.err.println(e); } } } } public static void fillEmailsHashSet(String line, HashSet<String> container) { Pattern p = Pattern .compile("([\w\-]([\.\w])+[\w]+@([\w\-]+\.)+[A-Za-z]{2,4})"); Matcher m = p.matcher(line); while (m.find()) { container.add(m.group(1)); } } }Before you run the program keep your EmailReader.java and emails.txt in a same folder D:\javaprogs before going to run our program.
Go to command prompt to compile our java packaged program
You can see the class compiled with the package name com.javabynataraj
then run the EmailReader program
The emails file named ouput is generated with all emails.
emails only filtered from the text file as shown below.
Reference Books: