Home > DeveloperSection > Blogs > Java I/O: The Line Count Program

Java I/O: The Line Count Program


Java Java  Java I/O 
Ratings:
0 Comment(s)
 159  View(s)
Rate this:

Java I/O: The Line Count Program


The Line Count program counts the number of lines in the specified text file. Java provides a class called LineNumberReader for this purpose. The LineNumberReader class wraps the BufferedReader class, which in turn wraps the Reader class.

Program Code

import java.io.*;

 

public class LineCounter {

                     public static void main(String[] args) {

                             LineNumberReader reader = null;

                             if (args.length < 1) {

                                      System.out.println("Usage: java LineCounter <filename>");

                                      System.exit(0);

                             }

                             try {

                                      reader = new LineNumberReader(new FileReader(args[0]));

                                      while (reader.readLine() != null) {

                                      }

                                      System.out.println("Line number of the last line in the file is: "

                                                          + reader.getLineNumber() + 1);

                             } catch (FileNotFoundException fe) {

                                      System.out.println(fe.getMessage());

                             } catch (IOException e) {

                                      System.out.println("Error reading file");

                             } finally {

                                      try {

                                                reader.close();

                                      } catch (Exception e) {

                                                e.printStackTrace();

                                      }

                             }

                     }

}

 

Explanation

·         The main method takes one command-line argument that specifies a text file. The program counts the number of lines in this text file and prints that number to the user console. The main method checks the appropriate usage of the command line and prints an appropriate message to the user if the command-line parameters are missing.

·         We open the file for reading by constructing a LineNumberReader class instance as follows:

                           reader = new LineNumberReader(new FileReader(args[0]));

·         After obtaining the reader object, the program simply reads the file line by line, ignoring the contents read:

                            while (reader.readLine() != null) {

                                  }

·         The readLine method returns null after reaching the end-of-file. At this time, it will have the count of the total number of lines in the file, which we print to the user’s console:

                                System.out.println("Line number of the last line in the file is: "

                                                                         + reader.getLineNumber() + 1);

We now close the file by calling the close method in the finally block. The entire file-handling code is enclosed in a try-catch block, and we provide appropriate exception handlers for all the required checked exceptions.

Output:

Some typical program output is shown here:

C:\360\io>java LineCounter MindStick.xml

Number of lines: 69

The Line Count program may also take a binary file as input and give us legitimate output. The reason behind this is how the readLine method operates. If we open the javadocs for the LineNumberReader class, we’ll see that this method reads a line of text. However, this could be a line containing non-ASCII data. The documentation also says that a line is considered to be terminated by any of the following: a linefeed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Thus, when we run this program on a non-ASCII data file, it counts the numbers of lines, as defined, and gives us legitimate output.


Don't want to miss updates? Please click the below button!

Follow MindStick