Program for reading all emails present in a file in Java

In this tutorial, we will learn how to read or fetch all the email ids from a .txt file in Java.

Searching and reading emails is a tough task. But, it is even more hectic when we are want to search and read one particular email from a set of the whole bunch of emails in a file. Doing so is already a hugely complicated task.

For example, we have a given file as an input file named for instance “myInputFile.txt”. The “myInputFile.txt” file carries a set of email IDs along with some other important data like mobile number, address and so on. We can imagine the heavy task we will come across if we try to search one particular email manually.

So we can optimize our task via reading the “myInputFile.txt” one to one that is line by line and as soon as we come across our desired email in any respective line, we can get it copied to an output file named for instance “myOutputFile.txt”.

Reading Emails of a File

We are very familiar with the concept of Regular expression. (Know Regex!). This concept is one of the easiest and simplest approaches to solve problems as above related to file operations. Hence, in order to find the email ID in our input file, we will deploy this concept in our topic.

For this, we need to know the criteria of a valid email ID. Below are mentioned all the criteria for a string to be a valid email ID.

A string can be a valid email ID if it satisfies the following:

  1. The first character should be either lowercase or uppercase alphabet or can even be any digit from 0 to 9.
  2. The rest character after the first character and till @ should be either lowercase or uppercase alphabets or can even have any digit from 0 to 9 or special symbol ‘_’ and ‘.’.
  3. Successfully satisfying the above two conditions, we must always have the symbol ‘@’.After @, the string should contain any lowercase or any uppercase alphabet or can even have any digit from 0 to 9 until ‘.’.
  4. After the string satisfies the above three conditions, the string should contain ‘.’ symbol. Hence now we are left with the last part of an email ID such that at the end, the string should have any lowercase or uppercase alphabet.

Now we are done with the conditions. It is time to know what is regular expression needed for the above conditions.

Below are mentioned the regex of the above conditions.

  1. For point no 1 above, we have the regular expression as [a-zA-Z0-9]
  2. Next, for 2, we have the regular expression as [a-zA-Z0-9_.]*
  3. Then 3 has the regular expression as @[a-zA-Z0-9]
  4. Now for point no 4, the regular expression is [.][a-zA-Z]

Therefore, when a particular string from the “myInputFile.txt” file matches with the regular expression shown above of the email ID, then the string is matched. Hence the string will be written to our “myOutputFile.txt”.

Let the “myInputFile.txt” has the following content in it:

[email protected]: qwert(123456789)
[email protected]: absent today. is her mobile numbile.
[email protected]@qwerty.asd123.
if you want to contribute to our portal pls contact us anytime through
Check our website.
Thank you.

Sample Code:

Below is a sample Java code for the implementation of the above topic of reading mails from the input file and getting an output file that contains only the email IDs.

// Java code to read all email ids present in given file 
import java.util.regex.*; 
class Emails { 
public static void main(String[] args) throws IOException { 
// email ids written to myOutputFile.txt file 
PrintWriter p = new PrintWriter("myOutputFile.txt"); 
// Regular expression for email id 
Pattern pat=Pattern.compile( "[a-zA-Z0-9]" + "[a-zA-Z0-9_.]" + "*@[a-zA-Z0-9]" + "+([.][a-zA-Z]+)+"); 
BufferedReader b = new BufferedReader(new FileReader("myInputFile.txt")); 
//reading myInputFile.txt file 
String l = b.readLine(); 
while (l != null) { 
Matcher mat = pat.matcher(l); 
while (mat.find()) { 
l = b.readLine(); 


The content of “myOutputFile.txt” on the execution of the above code is:

[email protected]
[email protected]
[email protected]
[email protected]

Know more: Reading and writing a file

Leave a Reply

Your email address will not be published. Required fields are marked *