Java program to read all Emails present in a Given file Last Updated : 19 Nov, 2022 Comments Improve Suggest changes Like Article Like Report 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 [email protected] . 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 [email protected] and my friend Tanu Jain having email id [email protected] contributing to geeksforgeeks. Output: [email protected] [email protected] [email protected] [email protected] Comment More infoAdvertise with us Next Article Java program to read all Emails present in a Given file B bishaldubey Follow Improve Article Tags : Java java-regular-expression Practice Tags : Java Similar Reads Read File Into an Array in Java In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method. To store the content of the file better use the collection storage type instead of a static array as we don't know the exact lines o 8 min read File canRead() method in Java with Examples The canRead()function is a part of the File class in Java. This function determines whether the program can read the file denoted by the abstract pathname. The function returns true if the abstract file path exists and the application is allowed to read the file.Function signature: public boolean ca 2 min read Reading a CSV file in Java using OpenCSV A Comma-Separated Values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g normally it is a comma â, â). OpenCSV is a CSV parser library for Java. OpenCSV supports all the basic CSV-type operations you are want to do. Java 7 is currently th 7 min read Java FileReader Class read() Method with Examples The read() method of FileReader class in Java is used to read and return a single character in the form of an integer value that contains the character's char value. The character read as an integer in the range of 0 to 65535 is returned by this function. If it returns -1 as an int number, it means 2 min read How to find and open the Hidden files in a Directory using Java Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information 3 min read Like