Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please solve using java code and take a screenshot of the code Lab File 1/0 & Exceptions Write a program that reads an input file
please solve using java code and take a screenshot of the code
Lab File 1/0 & Exceptions Write a program that reads an input file and print the input file data to an output file but with the following additions 1. each line in the outputfile 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 1 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); 1 } Output file public class Lab 1. 2. 3. public static void main(String[] args) throws FileNotFoundException 5. 6. 7. 8. 9. 10. 11. 12. 13. 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); 14. 15. 16. 17. 18. 19. 20. 21. } }Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started