Wednesday, July 3, 2024
HomeLanguagesJavaJava program to read all Emails present in a Given file

Java program to read all Emails present in a Given file

Given a file as input.txt containing some email ids which are mixed with other data. The task is to read this input file line by line and if any email id is found in that line, write that email id to another file, which is output.txt. Example:

Input: input.txt Output: output.txt

Approach: To detect the email id in that file, a simple solution is Regular expression. First, we have to form a regular expression for email id. Whenever any string in the input.txt file matches with that regular expression which we form for email id, then that matched string will be written to output.txt file. A string is said to be email id if that string follow below criteria:

  • The first character can be lowercase or uppercase alphabet or it can contain any digit from 0 to 9.For this criteria, regular expression [a-zA-Z0-9]
  • The rest character after the first character and until reaching @, the characters can be lowercase or uppercase alphabet or it can contain any digit from 0 to 9 or special symbol ‘_’ and ‘.’ .For this criteria, regular expression [a-zA-Z0-9_.]*
  • After the above two criteria, the string contain symbol ‘@’.After that string should contain any lowercase or uppercase alphabet or it can contain any digit from 0 to 9.For this criteria, regular expression @[a-zA-Z0-9]
  • After containing ‘@’ symbol, the string should contain ‘.’ symbol and after that the string should contain any lowercase or uppercase alphabet.For this criteria, regular expression [.][a-zA-Z]

Below is the implementation of the above approach: 

Java




// Java program to extract the
// email ids from a given text file
 
import java.util.regex.*;
import java.io.*;
 
class EmailIdExtraction {
    public static void extractEmailIds(
        BufferedReader br,
        PrintWriter pw,
        Pattern p)
    {
 
        String line = br.readLine();
 
        while (line != null) {
 
            Matcher m = p.matcher(line);
 
            // If any match
            while (m.find()) {
 
                // write the email id
                // to output.txt file
                pw.println(m.group());
            }
 
            // Goto next line in input.txt file
            line = br.readLine();
        }
 
        pw.flush();
    }
 
    // Driver code
    public static void main(String[] args)
        throws IOException
    {
 
        // PrintWriter for writing email id
        // to output.txt file
        PrintWriter pw
            = new PrintWriter("output.txt");
 
        // Compile() argument is the
        // regular expression for email id
        Pattern p
            = Pattern.compile(
                "[a-zA-Z0-9]"
                + "[a-zA-Z0-9_.]"
                + "*@[a-zA-Z0-9]"
                + "+([.][a-zA-Z]+)+");
 
        // BufferedReader for reading
        // from input.txt file
        BufferedReader br
            = new BufferedReader(
                new FileReader("input.txt"));
 
        // Calling extractEmailIds
        extractEmailIds(br, pw, p);
    }
}


Input:

Hello my name is Bishal Dubey and my email id is dubey.bishal159@gmail.com . Welcome to Geeksforgeeks, A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes . My brother, Bikash Dubey having email id bikashdubey42@gmail.com and my friend Tanu Jain having email id tanu_jain@gmail.com contributing to neveropen. If you like Lazyroar and would like to contribute, you can also mail your article to review-team@neveropen.co.za.

Output:

dubey.bishal159@gmail.com bikashdubey42@gmail.com tanu_jain@gmail.com review-team@neveropen.co.za

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments