Privacy Policy Terms Of Use. Copyright © 2006-2010 Java Tutorials and Examples.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
- /*
- Create a program which prints number of lines in the file. File name
- should be supplied using command line argument.
- */
- import java.io.*;
- class CountNumberOfLinesFile
- {
- public static void main(String args[])
- {
- try
- {
- FileReader fr = new FileReader(args[0]);
- BufferedReader br = new BufferedReader(fr);
- String line;
- int count = 0;
- while((line = br.readLine()) != null){
- count++;
- }
- System.out.println("There are " + count + " line(s) in the file.");
- fr.close();
- }
- catch(Exception e)
- {
- System.out.println("Exception : " + e);
- }
- }
- }

Post new comment