CJ_Slip_1B

Slip No. 1

B) Write a ‘java’ program to copy only non-numeric data from one file to another file.

1. Open Notepad.

2. Type the following code:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    public class CopyNonNumericData {
    public static void main(String[] args) {
    String inputFilePath = "input.txt";
    String outputFilePath = "output.txt";

    try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath));
    BufferedWriter writer = new BufferedWriter(new FileWriter(outputFilePath))) {

    int c;
    while ((c = reader.read()) != -1) {
    if (!Character.isDigit(c)) {
    writer.write(c);
    }
    }

    System.out.println("Non-numeric data has been copied successfully.");
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


3. Save the file with the name CopyNonNumericData.java. Make sure to select "All Files" in the "Save as type" dropdown and add the .java extension manually.

4. Open the Command Prompt.

5. Compile the Java program by typing:
javac CopyNonNumericData.java

6. Run the compiled Java program by typing:
java CopyNonNumericData

No comments:

Post a Comment