1 27 package org.htmlparser.tests.codeMetrics; 28 29 import java.io.BufferedReader ; 30 import java.io.File ; 31 import java.io.FileFilter ; 32 import java.io.FileReader ; 33 34 public class LineCounter { 35 36 public int count(File file) { 37 System.out.println("Handling "+file.getName()); 38 int count = 0; 39 if (file.isDirectory()) { 41 count = recurseDirectory(file, count); 43 } else { 44 count = countLinesIn(file); 46 } 47 return count; 48 } 49 50 55 public int countLinesIn(File file) { 56 int count = 0; 57 System.out.println("Counting "+file.getName()); 58 try { 59 BufferedReader reader = new BufferedReader (new FileReader (file.getAbsolutePath())); 60 String line = null; 61 do { 62 line = reader.readLine(); 63 if (line!=null && 64 line.indexOf("*")==-1 && 65 line.indexOf("//")==-1 && 66 line.length()>0 67 ) count++; 68 } 69 while (line!=null); 70 } 71 catch (Exception e) { 72 e.printStackTrace(); 73 } 74 return count; 75 } 76 77 public int recurseDirectory(File file, int count) { 78 File [] files = file.listFiles(new FileFilter () { 79 public boolean accept(File file) { 80 if (file.getName().indexOf(".java")!=-1 || file.isDirectory()) { 81 return true; 82 } else { 83 return false; 84 } 85 } 86 }); 87 for (int i=0;i<files.length;i++) { 88 count += count(files[i]); 89 } 90 return count; 91 } 92 93 public static void main(String [] args) { 94 LineCounter lc = new LineCounter(); 95 System.out.println("Line Count = "+lc.count(new File (args[0]))); 96 } 97 } 98 | Popular Tags |