Open In App

Java Program to Read All Mobile Numbers Present in Given File

Last Updated : 09 May, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In this article, we are going to discuss a Java program that reads all mobile numbers from a given text file and writes the correct mobile number to another output file. For this, we are going to use regular expression to identify the correct mobile number.

Note: The number should meet the correct criteria, such as starting with a digit between 7 and 9.

The image below demonstrates the extraction of valid numbers from a text input and saving them to an output file.


Mobile Number Validation Criteria

The mobile number should meet the criteria which are listed below:

  • The first number must be a number between 7 and 9.
  • The rest of the 9 digits can be any number between 0 and 9.
  • The mobile number can have 11 digits, including 0 at the start.
  • The mobile number can have 12 digits, including 91 at the start.
  • We will read the input file line by line, and whenever we find a number

Note: Whenever we find a valid number which matches the above criteria, we will write that number to another file.

Now, we are going to discuss the approach for solving this.

Read All Mobile Numbers Present in Given File in Java

We are going to use regular expressions to match valid mobile numbers. The number should start with 7, 8, or 9, it can also start with 0 and 91.

Steps:

  • Open and read the input.txt file.
  • Going to use regular expression to find the valid numbers.
  • Write the correct mobile number that matches the criteria to an output.txt file.

input.txt:

Abstract classes may have some executable methods

and 7873923408 methods left unimplemented. Interfaces

contain no implementation code. An abstract

class can have non abstract 7205435005 methods.

All methods 1234 of an interface are abstract.

Example:

Java
// Java program to demonstrate how to read 
// all mobile number in a given file
import java.io.*;
import java.util.regex.*;

class Geeks {
    public static void main(String[] args)
        throws IOException
    {
        // Using try-with-resources to handle resource
        // management Automatically handles file closing
        
        try (PrintWriter pw = new PrintWriter("output.txt");
        
             // Automatically handles file closing
             BufferedReader br = new BufferedReader(
                 new FileReader("input.txt"))) {

            // Regular expression for mobile number
            Pattern p
                = Pattern.compile("(0|91)?[7-9][0-9]{9}");

            String line = br.readLine();
            while (line != null) {
                Matcher m = p.matcher(line);

                while (m.find()) {
                    // Write the mobile number to output.txt
                    // file
                    pw.println(m.group());
                }

                line = br.readLine();
            }
        }
    }
}

Output:

7873923408
7205435005

Explanation:

  • Here, the regular expression is used to check the valid mobile number. This regular expression checks if the number optionally starts with 0 or 91. It is must that the number should start with digit between 7 and 9, followed by 9 more digits between 0 and 9.
  • The line BufferedReader br = new BufferedReader(new FileReader("input.txt")) is used to read the input file line by line.
  • And then the line PrintWriter pw = new PrintWriter("output.txt") is used to write the correct mobile number to an output file.

Similar Reads