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

Write a program that reads an input file then print the input file data to an ou

ID: 3828925 • Letter: W

Question

Write a program that reads an input file then print the input file data to an output file but with the following additions

1. each line in the outpfile is prefixed with the current line number, first line with 1. Second line with 2. etc

2. all empty lines or lines containing all blanks are not to be written to the output file

3. The program must not throw a FileNotFoundException, rather it should catch the exception in the program and ask the user again for a new input file name. Keep repeating until no exception is generated from opening the file to read and write.

Sample run if file Lab.java does not exist and Lab4.java exists

Please enter input file name: Lab.java

Please enter outputfile name: Lab.out

File does not exist

Please enter input file name: Lab4.java

Please enter outputfile name: Lab.out

Input File :

public class Lab4

{

   

    public static void main(String[] args) throws FileNotFoundException

    {

       

      String a = "c3.txt";

      String x = "xyz.txt";

      File b = new File(a);

      Scanner in = new Scanner(b);

      Scanner key = new Scanner(System.in);

      String data = "";

     

      while (in.hasNextLine())

      {

         data += in.nextLine();

      }

     

      String d = key.next();

      d = key.next();

      d = d + data;

      System.out.println(d);

     

    }

}

Output file

1.         public class Lab

2.    {

3.    public static void main(String[] args) throws FileNotFoundException

4.       {

5.          String a = "c3.txt";

6.          String x = "xyz.txt";

7.          File b = new File(a);

8.          Scanner in = new Scanner(b);

9.          Scanner key = new Scanner(System.in);

10.        String data = "";

11.       while (in.hasNextLine())

12.         {

13.            data += in.nextLine();

14.         }

15.         String d = key.next();

16.      d = key.next();

17.         d = d + data;

18.         System.out.println(d);

19.        

20.       }

21.   }

Explanation / Answer

ReadFileAddLineNo.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class ReadFileAddLineNo {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.print("Please enter input file name: ");
       String input = scan.next();
       System.out.print("Please enter outputfile name: ");
       String output = scan.next();
       File inputFile = new File(input);
       File outputFile = new File(output);
      
       while(!inputFile.exists()){
           System.out.println("File does not exist");
           System.out.print("Please enter input file name: ");
           input = scan.next();
           System.out.print("Please enter outputfile name: ");
           output = scan.next();
           inputFile = new File(input);
           outputFile = new File(output);
          
       }
       try{
       Scanner process = new Scanner(inputFile);
       PrintWriter pw = new PrintWriter(outputFile);
       int lineNo = 0;
       while(process.hasNextLine()){
           String line = process.nextLine().trim();
           System.out.println(line);
           if(!line.equals("")){
               lineNo++;
               pw.write(lineNo+". "+line+" ");
           }
       }
       pw.flush();
       pw.close();
       } catch(FileNotFoundException e) {
           System.out.println(e);
       }
   }

}

Output:

Please enter input file name: D:\Lab4.java
Please enter outputfile name: D:\Lab.out

1. import java.io.File;
2. import java.io.FileNotFoundException;
3. import java.util.Scanner;
4. public class Lab4
5. {
6. public static void main(String[] args) throws FileNotFoundException
7. {
8. String a = "c3.txt";
9. String x = "xyz.txt";
10. File b = new File(a);
11. Scanner in = new Scanner(b);
12. Scanner key = new Scanner(System.in);
13. String data = "";
14. while (in.hasNextLine())
15. {
16. data += in.nextLine();
17. }
18. String d = key.next();
19. d = key.next();
20. d = d + data;
21. System.out.println(d);
22. }
23. }

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