Thursday, November 20, 2025
HomeLanguagesJavaHow to Extract Email Addresses, Phone Numbers from a CSV File using...

How to Extract Email Addresses, Phone Numbers from a CSV File using Java?

CSV stands for Comma Separated Values. A CSV file is a plain text file. CSV file is used to store the records. CSV File records are stored with delimiter separator value.

Problem Statement

A CSV file is given. The task is to read and extract phone numbers, and email ids from the given CSV file, and return the list of phone numbers and emails as output.

User.csv

 

Approach

Step 1: Read the CSV file

A CSV file can be read line by line with the help of readLine() Method of BufferedReader class.

Step 2: After reading the CSV file the task is to validate the phone numbers and email Ids in the Given CSV File. To validate the phone number and email Id we can use Regular Expressions:

  • Regular Expression for Phone numbers: (0|91)?[6-9][0-9]{9}
  • Regular Expression for EmailIds: ^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$

Below is the code implementation of the above-discussed approach.

Java




import java.io.*;
import java.util.*;
 
public class CSvReaderTest {
   
    public static final String delimeter = ",";
   
    public static void read(String csvFile)
    {
        List<String> phoneNumbers = new ArrayList<String>();
        List<String> emailIds = new ArrayList<String>();
        try {
            File file = new File(csvFile);
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line = "";
            String tempArray[];
 
            while ((line = br.readLine()) != null) {
                tempArray = line.split(delimeter);
                for (String tempStr : tempArray) {
                    // Regex pattern for matching Phone
                    // Numbers
                    if (tempStr.matches(
                            "(0|91)?[6-9][0-9]{9}")) {
                        phoneNumbers.add(tempStr);
                    }
                    // Regex pattern for matching Email Ids
                    else if (
                        tempStr.matches(
                            "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$")) {
                        emailIds.add(tempStr);
                    }
                }
            }
            System.out.println("List of PhoneNumbers are:"
                               + phoneNumbers);
            System.out.println("List of Email Ids are:"
                               + emailIds);
            br.close();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    public static void main(String args[])
    {
        String csvFile
            = "C:\\Users\\Rahul Chauhan\\Downloads\\FirstCSV.csv";
        CSvReaderTest.read(csvFile);
    }
}


Note: This program cannot be run through the online compiler because the csv file needs to be present on the local System. The csv file path is imported to get the result as Output.

Output:

List of PhoneNumbers are:[9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900, 9123478900]
List of Email Ids are:[UserFirst00.00@gmail.com, UserFirst01.01@gmail.com, UserFirst02.02@gmail.com, UserFirst03.03@gmail.com, UserFirst04.04@gmail.com, 
UserFirst05.05@gmail.com, UserFirst06.06@gmail.com, UserFirst07.07@gmail.com, UserFirst08.08@gmail.com, UserFirst09.09@gmail.com, UserFirst10.10@gmail.com]
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32404 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6776 POSTS0 COMMENTS
Nicole Veronica
11924 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11994 POSTS0 COMMENTS
Shaida Kate Naidoo
6904 POSTS0 COMMENTS
Ted Musemwa
7160 POSTS0 COMMENTS
Thapelo Manthata
6859 POSTS0 COMMENTS
Umr Jansen
6846 POSTS0 COMMENTS