Slip No 18 Q B

Q.Write a java program to copy the data from one file into another file, while copying change the case of characters in target file and replaces all digits by ‘*’ symbol. [25 M]

import java.io.*;

class FileCopyModify
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader("source.txt"));
        BufferedWriter bw = new BufferedWriter(new FileWriter("target.txt"));

        int ch;

        // Read character by character
        while((ch = br.read()) != -1)
        {
            char c = (char) ch;

            // Replace digits with '*'
            if(Character.isDigit(c))
            {
                bw.write('*');
            }
            // Convert uppercase to lowercase
            else if(Character.isUpperCase(c))
            {
                bw.write(Character.toLowerCase(c));
            }
            // Convert lowercase to uppercase
            else if(Character.isLowerCase(c))
            {
                bw.write(Character.toUpperCase(c));
            }
            else
            {
                bw.write(c);
            }
        }

        br.close();
        bw.close();

        System.out.println("File copied successfully with modifications.");
    }
}

source.txt

Hello abc123
Java File45

target.txt

hELLO kOMAL***
jAVA fILE**
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top