- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Read All the Emails Present in a File in Java
As per the problem statement we need to find all the Emails present in the file.
Let’s explore the article to see how it can be done by using Java programming language.
To Show You Some Instances
Instance-1
Suppose there is given paragraph and it contains many different email ids and we want to extract all the mail id present in that paragraph. For e.g. −
“I am a boy with email boy54@gmail.com. My friend’s email is friend78@gmail.com.”.”
From the above paragraph, we need to take out the given two mail id in the paragraph i.e.,
boy54@gmail.com and friend78@gmail.com.
Instance-2
Suppose there is given paragraph and it contains many different email ids and we want to extract all the mail id present in that paragraph. For e.g.
“Hey, I am Rakesh Agarwal, my mail id is rakesh@tutorialspoint.com. My friend’s email is prakash@gmail.com.”
From the above paragraph we need to take out the given two mail id in the paragraph i.e.,
rakesh@tutorialspoint.com and prakash@gmail.com.
Instance-3
Suppose there is given paragraph and it contains many different email ids and we want to extract all the mail id present in that paragraph. For e.g.
“Hey, I am Rakesh Agarwal, my mail id is rakesh@yahoo.com.
From the above paragraph we need to take out the given mail id in the paragraph i.e.,
rakesh@yahoo.com.
Algorithm
Step 1 − Import the necessary java libraries.
Step 2 − Define the regular expression for the matching pattern.
Step 3 − Assign the location of the text file or take input from the user.
Step 4 − Matching the required format using matcher method.
Syntax
Compile() - The compile() method is used to match text or expression against a regular expression more than one time. The compile() method is used to compile the given regular expression passed as the string. It belongs to the Pattern class.
matcher() - The matcher() method performs match operations on a character sequence by interpreting a Pattern.
readLine() - The readLine() method is used to read a given line from text. It takes input from the console screen and it belongs to the BufferReader class.
Multiple Approaches
We have provided the solution in different approaches.
By Using Input from File
By Using User Input from Console
Let’s see the program along with its output one by one.
Approach-1: By Using Input from File
Example
In this method we will take input from the system itself as a text file and filter out all the email id present in that text file.
import java.util.regex.*; import java.io.*; public class Main { public static void main(String[] args) throws IOException { // Regular expression for email id and assigning it to pattern method Pattern pat=Pattern.compile( "[a-zA-Z0-9]" + "[a-zA-Z0-9_.]" + "*@[a-zA-Z0-9]" + "+([.][a-zA-Z]+)+"); //Assigning the location of the text file BufferedReader b = new BufferedReader(new FileReader("E:\file1.txt")); //reading yo.txt file String l = b.readLine(); while (l != null) { //Matching the required format using matcher method Matcher mat = pat.matcher(l); while (mat.find()) { System.out.println(mat.group()); } l = b.readLine(); } } }
Output
abhaprakash@123.com abhaprakash@tutorialspoint.com rancho45@gmail.com y@gmail.com asifa.sandal120098@yahoo.com welcometoCodespeedy@hotmail.com.abcxyz 12345@qwerty.asd
Approach-2: By Using Input from Console
Example
In this method we will take input from the user and filter out all the email id present in it.
import java.util.regex.*; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { // Regular expression for email id and assigning it to pattern method Pattern pat=Pattern.compile( "[a-zA-Z0-9]" + "[a-zA-Z0-9_.]" + "*@[a-zA-Z0-9]" + "+([.][a-zA-Z]+)+"); //Taking input from the console Scanner b = new Scanner(System.in); System.out.println("Write the Paragraph containing email ids"); //reading yo.txt file String l = b.nextLine(); System.out.println("\nEmail ids are-"); while (l != null) { //Matching the required format using matcher method Matcher mat = pat.matcher(l); while (mat.find()) { System.out.println(mat.group()); } l = b.nextLine(); } } }
Output
Write the Paragraph containing email ids abhaprakash@123.com abhaprakash@com abhaprakash$gmail.com abha abhaprakash@tutorialspoint.com rancho45@gmail.com y@gmail.com asifa.sandal120098@yahoo.com welcometoCodespeedy@hotmail.com.abcxyz 12345@qwerty.asd Email ids are- abhaprakash@123.com abhaprakash@tutorialspoint.com rancho45@gmail.com y@gmail.com asifa.sandal120098@yahoo.com welcometoCodespeedy@hotmail.com.abcxyz 12345@qwerty.asd
In this article, we explored how to read all the emails present in the file in Java.
To Continue Learning Please Login
Login with Google