Write a Java program to perform the following tasks: The program should ask the user for the name of an input file and the name of an output file. It should then open the input file as a text file (use the try block to check if the input file exists) and read the contents line by line. It should also open the output file as a text file and write to it the lines in the input file, prefixed by line numbers starting at 1. So if an input file named "fred.txt" contains: Hello, I am Fred. I am enrolled in a class. Bye and the user enters "fred.txt" and "fredNum.txt", then after running the program "fredNum.txt" will contain something like: 1 Hello, 2 3 I am Fred. 4 I am enrolled in a class. 5 6 Bye Finally, the program should display on the screen your name, the name of the output file, followed by a count of the total number of lines, the total number of words, and the total number of characters in the input file. For the above input file, the following will be displayed to the screen: My name = Joe Bloggs Name of Output file = fredNum.txt Total number of lines in fred.txt = 6 Total number of words in fred.txt = 10 Total number of characters in fred.txt = 43
Write a Java program to perform the following tasks:
The program should ask the user for the name of an input file and the name of
an output file. It should then open the input file as a text file (use the try block
to check if the input file exists) and read the contents line by line. It should
also open the output file as a text file and write to it the lines in the input file,
prefixed by line numbers starting at 1.
So if an input file named "fred.txt" contains:
Hello,
I am Fred.
I am enrolled in a class.
Bye
and the user enters "fred.txt" and "fredNum.txt", then after running the
program "fredNum.txt" will contain something like:
1 Hello,
2
3 I am Fred.
4 I am enrolled in a class.
5
6 Bye
Finally, the program should display on the screen your name, the name of the
output file, followed by a count of the total number of lines, the total number
of words, and the total number of characters in the input file.
For the above input file, the following will be displayed to the screen:
My name = Joe Bloggs
Name of Output file = fredNum.txt
Total number of lines in fred.txt = 6
Total number of words in fred.txt = 10
Total number of characters in fred.txt = 43
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images