Java Line Count - returns number of lines in a file

import java.io.*;
 
public class LineCount
{
    public static int lineCount(String fileName) {
        File fin = new File(fileName);
        int lines = 0;
        if (fin.exists()) {
            try {
                BufferedReader br = new BufferedReader(new FileReader(fin));
                String oneLine;
                while ((oneLine = br.readLine()) != null) {
                    lines++;
                }
                return lines;
            }
            catch (IOException e) {
                System.err.println(e);
            }
        }
        return 0;
    }
 
    public static void main(String args[]) {
        if (args.length > 0) {
            System.out.println(LineCount.lineCount(args[0]));
        }
    }
}
javalinecount.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0