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.)
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 PreviewSolution 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.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
    void readAndWrite() throws IOException{
       Scanner input = new Scanner(inputStream);
       PrintWriter pw = new PrintWriter(outputStream);
       int count = -1;
             int lenght   = input.nextLine().length();
                pw.printf("%d          %d",count,lenght);

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats