Question: Create working Java code based on the following guidelines. This program counts characters in each line of a text file, and writes the line number
Create working Java code based on the following guidelines.
This program counts characters in each line of a text file, and writes the line number and total characters of each line to a second text file. It also displays on the screen the total number of lines and characters read from the first text file. (See the table below for a sample input file, and the resulting output file.)
Use the 2 arg constructor in your program. That constructor sets two of the attributes. The no-arg constructor can be left empty.
The readAndWrite method: --Creates a Scanner object from the FileInputStream object. --Create a PrintWriter object from the FileOutputStream object. --Using the Scanner object, read each line in the input. (Hint: loop using the hasNext() method.) --Using the PrintWriter object, write the output file. Write one line of output for each line of input. Each line of the output file will have the line number (starting with line 0, of course) and the number of characters in the corresponding line of the input file. (See sample below.)
The output file will be neatly formatted in columns.
The main method will: --Ask the user for an input file name and output file name. (See the Sample run.) --Create FileInputStream and FileOutputStream objects. --Create a CharCounter object. --Use the CharCounter object to process the input file and write the output file. --Display a message with the total line and character counts. --Will need throws IOException in the method header.
Both input and output files will be text files.
Sample run #1
(User input in bold)
Enter the name of the input file: in.txt
Enter the name of the output file: out.txt
The input file has 3 lines and 24 total characters.
File contents:
| in.txt | out.txt |
| the end is near the end 30 | 0 15 1 7 2 2 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
