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

Write a java program that would ask the user to enter an input file name, and an

ID: 3795428 • Letter: W

Question

Write a java program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and then writes the content of the input file to the output file with each line proceeded with a line number followed by a colon. The line numbering should start at 1.

should be used the following technique
(a) while loop should be used to complete the program. (b)Scanner classisused
(c) PrintWriter class is used
(d) The files are closed before the program terminates.

Example

Original Input file Output file with line number added 234 1: 234 546 2: 546 238 3: 238 765 4: 765

Explanation / Answer

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.util.Scanner;

/**

*

*/

/**

* @author Srinivas Palli

*

*/

public class ReadFileWriteEvens {

   /**

   * @param args

   */

   public static void main(String[] args) {

       // TODO Auto-generated method stub

       Scanner scanner = null;

       try {

           scanner = new Scanner(System.in);

           // prompt enter the file name

           System.out.print("Ente the input file name:");

           String fileName = scanner.nextLine();

           // prepare output file

           File outFile = new File("output.txt");

           if (!outFile.exists()) {

               outFile.createNewFile();

           }

           FileWriter fw = new FileWriter(outFile.getAbsoluteFile());

           BufferedWriter bw = new BufferedWriter(fw);

           scanner = new Scanner(new File(fileName));

           while (scanner.hasNext()) {

               String nextWord = scanner.next();

               try {

                   int num = Integer.parseInt(nextWord);

                   // write if it is even

                   if (num % 2 == 0)

                       bw.write(num + " ");

               } catch (Exception e) {

                   // TODO: handle exception

               }

           }

           bw.close();

           scanner.close();

       } catch (Exception e) {

           // TODO: handle exception

       }

   }

}

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