Write a ‘java’ program to copy only non-numeric data from one file to another file | EasyCoding45

Write a 'java' program to copy only non-numeric data from one file to another file.


Below is a simple Java program that reads data from one file, filters out numeric characters, and then writes the non-numeric data to another file.

Program : (Write a ‘java’ program to copy only non-numeric data from one file to another file)


import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;


public class CopyNonNumericData {

    public static void main(String[] args) {

        String inputFile = "C:/Users/LENOVO/Desktop/Java Programs/input.txt";    // Replace with the path to your input file

        String outputFile = "C:/Users/LENOVO/Desktop/Java Programs/output.txt";  // Replace with the path to your output file


        try (FileReader reader = new FileReader(inputFile);

             FileWriter writer = new FileWriter(outputFile)) {


            int character;

            while ((character = reader.read()) != -1) {

                char ch = (char) character;


                // Check if the character is non-numeric

                if (!Character.isDigit(ch)) {

                    // Write non-numeric characters to the output file

                    writer.write(ch);


                    // Print non-numeric characters to the terminal

                    System.out.print(ch);

                }

            }


            System.out.println("\nNon-numeric data copied successfully.");


        } catch (IOException e) {

            System.err.println("Error: " + e.getMessage());

        }

    }

}

Note: Make sure to replace the input.txt and output.txt with the actual file paths you want to use. This program uses FileReader to read characters from the input file and FileWriter to write non-numeric characters to the output file. It uses Character.isDigit() to check if a character is numeric or not.

Output :

https://easycoding45.blogspot.com/2023/11/write-java-program-to-copy-only-non.html

input.txt
https://easycoding45.blogspot.com/2023/11/write-java-program-to-copy-only-non.html

output.txt
https://easycoding45.blogspot.com/2023/11/write-java-program-to-copy-only-non.html








Comments