Question
1. The package name is specified in the assignment statement
2. Other names are specified in the UML diagram
3. The constructor sets two of the attributes. There is only one constructor.
4. The readAndWrite method:
a. Create a Scanner object from the FileInputStream object.
b. Create a PrintWriter object from the FileOutputStream object.
c. Using the Scanner object, read each line in the input. (Hint: loop using the hasNext() method.)
d. 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.)
e. The output file will be neatly formatted in columns.
5. The two getters return the respective attributes.
6. The main method will:
a. Ask the user for an input file name and output file name. (See the Sample run.)
b. Create FileInputStream and FileOutputStream objects.
c. Create a CharCounter object.
d. Use the CharCounter object to process the input file and write the output file.
e. Display a message with the total line and character counts.
f. Will need “throws IOException” in the method header.
7. Both input and output files will be text files. You can read them with your IDE, Notepad, or any text editor.
8. If you don’t specify a path, the files will be in the project folder. The project folder is usually specified next to the project name in the top line of the project pane in the IDE.
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.
Solution Preview
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.
import java.io.FileInputStream;import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.util.Scanner;
public class CharCounter {
private FileInputStream inputStream;
private FileOutputStream outputStream;
private int lineCount;
private int charCount;
//Constructor to inialize the file input and output stream
CharCounter(FileInputStream fin, FileOutputStream fout) {
inputStream = fin;
outputStream = fout;
}
/*
This method is use to read the file line by line throw Scanner having input stream object and
writing to the output file throw PrintWriter having output stream object.
input.hasNext() return true if they have a line after leaving the current line and
input.nextLine().length returns the lenght of the line and pw.printf is used to write in the output
file.
*/
void readAndWrite() throws IOException{
Scanner input = new Scanner(inputStream);
PrintWriter pw = new PrintWriter(outputStream);
int count = -1;
while(input.hasNext()){
int lenght = input.nextLine().length();
count++;
pw.printf("%d %d",count,lenght);
pw.println();
}
pw.close();
}...
By purchasing this solution you'll be able to access the following files:
Solution.zip.