Java Program to Read All Mobile Numbers Present in Given File
Last Updated :
09 May, 2025
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
Java program to read all mobile numbers present in given file 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 c
3 min read
Java program to read all Emails present in a Given file 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
3 min read
Java Program to Check For a Valid Mobile Number In Java, validating mobile numbers is usually done by using Regular Expressions (Regex) that define patterns for matching strings. Mobile number validation can be implemented for both local (Indian) and global phone numbers.Example: Now, let's take a basic example, to check if a mobile number is val
3 min read
Java Program to Print all the Strings that Match a Given Pattern from a File Figure a way out to print all the strings that match a given pattern from a file. To get closer to the goal, concepts of files should be crystal clear so as to think out a way to print strings later on implementing the same to reach the final goal. So prior to dealing with the strings urgency is to
5 min read
Java Program to Read a Large Text File Line by Line As we are well verse with this topic let us put more stress in order to figure out minute differences between them. Here we are supposed to read from a file on the local directory where a text file is present say it be 'gfg.txt'. Let the content inside the file be as shown below: Geeks for Geeks. A
3 min read
Java Program to Count Number of Digits in a String The string is a sequence of characters. In java, objects of String are immutable. Immutable means that once an object is created, it's content can't change. Complete traversal in the string is required to find the total number of digits in a string. Examples: Input : string = "GeeksforGeeks password
2 min read
Java Program to Read All Mobile Numbers Present in Given File
min read