package com.mailTest; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; public class CodeLineCounter { private static int totalLine; private static String fileToBeCounted[] = { "java", "jsp", "css", "html", "htm","xml"};//所需計算的源碼文件類型 private static int fileToBeCountedN = 6;//所需計算的源碼文件類型個數 private static boolean isFileToBeCounted(String type) { for (int i = 0; i < fileToBeCountedN; i++) { if (type.equals(fileToBeCounted[i])) return true; } return false; } private static String getType(String filename) { //System.out.println(filename); byte b[] = filename.getBytes(); byte[] type = new byte[10]; String rts = null; int i = 0, p = 0, n = filename.length(); for (i = 0; i < n; i++) { if (b[i] == '.') { p = i; break; } } //System.out.print(p); i = p + 1; p = 0; for (; i < n&&p<10; i++) { type[p++] = b[i]; //System.out.print(b[i]); } rts = new String(type); //System.out.println(rts.substring(0, p)); return rts.substring(0, p); } private static void countLine(String path) { File file = new File(path); File lists[] = file.listFiles(); for (int i = 0; i < lists.length; i++) { if (lists[i].isFile()) { String filename = lists[i].getName(); boolean isFileToBeCounted = isFileToBeCounted(getType(filename)); if (isFileToBeCounted) { try { int lines=0; FileReader read = new FileReader(path + filename); BufferedReader br = new BufferedReader(read); String row; while ((row = br.readLine()) != null) { lines++; } System.out.println("文件:" + path + filename+"共:"+lines+"行代碼;"); totalLine+=lines; } catch (Exception e) { e.printStackTrace(); } } } else { String paths = path; paths = paths + lists[i].getName() + "\\"; System.out.println("進入目錄:" + paths+";"); countLine(paths); } } } public static void main(String[] args) { totalLine = 0; String path = new String("F:\\ppp\\");//項目的絕對地址 countLine(path); System.out.println("整個項目共:"+totalLine+"行代碼;"); } }