Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The application should be run from the command line using command-line arguments

ID: 3622547 • Letter: T

Question

The application should be run from the command line using command-line arguments to provide the input and output filenames.
The application should display an appropriate error message if the user forgets to enter the input and output filenames at the command prompt or if the files are not found.
The application should open the file and read each line of the file.
For each line read, the application should determine the total number of characters in the line, including white-space characters, determine the number of times the number 4 appears in the line, replace all occurrences of the string "this" with "That", and finally reverse the order of the characters in the line.
The output file should contain the same number of lines as the input file with the following content for each line:
total number of characters, including whitespace characters, in the line
number of times the number 4 appeared in the line
reversed string (for example, "cats" in the input file would be displayed as "stac")
The items on each line in the output file should be separated by commas (for example: for input line "this4cats", the output line should be "9,1,stac4tahT")

The application should close any open files before exiting.

Explanation / Answer

Usage:

Compile file using

javac CopyFile.java

It will produce a class file CopyFile.class in same directory.

Contents of input file in.txt:

this4cats this4cats

this4cats this4cats

this4cats this4cats

Save this file in same directory of class file.

Run using:

java CopyFile in.txt out.txt

The command will execute silently and a new file out.txt would be created in same directory.

Contents of out.txt

19,2,stac4tahT stac4tahT
19,2,stac4tahT stac4tahT
19,2,stac4tahT stac4tahT

Now Code will go here.

// save following code as CopyFile.java and follow above steps.


import java.io.*;
import java.util.*;

public class CopyFile {

    public static void main(String args[]) throws IOException {
        Scanner inFile= null;
        BufferedWriter outFile = null;
        String linesCount[];
        int count4 = 0;
        if (args.length != 2) {
            System.out.println("usage: java CopyFile srcfile dstfile");
            return;
        }
        try {

            inFile= new Scanner(new FileReader(args[0]));
            outFile = new BufferedWriter(new FileWriter(args[1]));
            int linesInFile = 0;
            //counting number of lines in
            while (inFile.hasNextLine()) {
                linesInFile++;
                inFile.nextLine();
            }
            //creating string array equal to the lines of input file.
            linesCount = new String[linesInFile];
            linesInFile = 0;
            //file closed
            inFile.close();
            //open again to read from begining
            inFile= new Scanner(new FileReader(args[0]));
            //storing lines of file in array
            while (inFile.hasNextLine()) {
                linesCount[linesInFile++] = inFile.nextLine();
            }
            //loop till length of array lines
            for (int i = 0; i < linesCount.length; i++) {
                //loop till line[i]
                for (int j = 0; j < linesCount[i].length(); j++) {
                    //counting number of 4s
                    if (linesCount[i].charAt(j) == '4') {
                        count4++;
                    }
                }
                //writing to file
                outFile.write(linesCount[i].length() + "," + count4 + "," + new StringBuffer((linesCount[i]).replaceAll("this", "That")).reverse().toString());
                outFile.newLine();
                //resetting number of 4s to start afresh
                count4 = 0;
            }
            outFile.close();
            inFile.close(); //proper shutdown of file buffers
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote