Open In App

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:

Below is the implementation of the above approach: 







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 geeksforgeeks.

Output:

dubey.bishal159@gmail.com bikashdubey42@gmail.com tanu_jain@gmail.com review-team@geeksforgeeks.org

Article Tags :