1 16 17 18 package org.apache.jetspeed.util; 19 20 import java.io.File ; 21 import java.io.FileReader ; 22 import java.io.BufferedReader ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 26 public class LineCounter 27 { 28 int count = 0; 29 30 public static void main( String args[] ) 31 { 32 LineCounter lc = new LineCounter(); 33 int count = 0; 34 int totalCount = 0; 35 for (int ix = 0; ix < args.length; ix++) 36 { 37 count = lc.run(args[ix]); 38 System.out.println( "Count for path [" + args[ix] + "] = " + count ); 39 totalCount += count; 40 } 41 System.out.println( "Total Count = " + totalCount ); 42 } 43 44 public int run(String path) 45 { 46 System.out.println("Running LineCounter for " + path ); 47 count = 0; 48 return traverse(path); 49 } 50 51 private int traverse(String path) 52 { 53 File file = new File (path); 54 if (file.isFile()) 55 { 56 try 57 { 58 String name = file.getName(); 59 if (name.endsWith("java")) 60 count += countFile(file); 61 } 62 catch (Exception e) 63 { 64 System.err.println("Failed to count file: " + path + " : " + e.toString()); 65 } 66 } 67 else if (file.isDirectory()) 68 { 69 if (!path.endsWith(File.separator)) 70 path += File.separator; 71 72 String list[] = file.list(); 74 75 for(int ix = 0; list != null && ix < list.length; ix++) 77 traverse(path + list[ix]); 78 79 80 } 81 return count; 82 } 83 84 private int countFile( File file ) 85 throws FileNotFoundException , IOException 86 { 87 FileReader reader = new FileReader ( file ); 89 BufferedReader br = new BufferedReader ( reader ); 90 String s; 91 int mycount = 0; 92 93 while ((s = br.readLine()) != null) 94 { 95 if (s.length() > 0) 96 mycount++; 97 } 98 return mycount; 99 } 100 } 101 102 | Popular Tags |