統計源檔案夾中代碼的行數,統計程式碼數
public class LineCounter {public static void main(String[] args) {String path = "D:/workspace/LineCounter";int count = getAllJavaFilesLineCount(new File(path));System.out.println("總行數:" + count);}/** * 使用遞迴實現統計這個檔案夾中(包含子孫檔案夾中的)的所有.java檔案的總行數 * * @param dir * 檔案夾 * @return */private static int getAllJavaFilesLineCount(File dir) {int count = 0;for (File file : dir.listFiles()) {// 如果是.java檔案,就統計行數if (file.isFile() && file.getName().endsWith(".java")) {count += FileUtils.getLineCount(file);}// 如果是檔案夾,就遞迴調用else if (file.isDirectory()) {count += getAllJavaFilesLineCount(file);}}return count;}}
public class FileUtils {/** * 讀取指定檔案的內容,返回總行數 * * @param file * @return */public static int getLineCount(File file) {if (!file.isFile()) {throw new IllegalArgumentException("請指定一個有效檔案對象!");}try {BufferedReader reader = new BufferedReader(new FileReader(file));int count = 0;while (reader.readLine() != null) {count++;}reader.close();return count;} catch (Exception e) {throw new RuntimeException(e);}}}
c#問題,好新人給個思路 用c#統計檔案夾下所有檔案的程式碼數
string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;//擷取項目路徑
string FolderPath = AppPath + Server.MapPath("你的檔案夾名");//擷取檔案夾路徑
string[] FilesArray = System.IO.Directory.GetFiles(FolderPath,"*");//擷取所有檔案
foreach (string file in FilesArray) {//遍曆檔案
try {
StreamReader txtRe= new StreamReader(FolderPath + "\\" + file);//開啟當前檔案
string[] txtlist = txtRe.ReadToEnd().Split('\n');//txtlist.Length就是行數
foreach (string txt in txtlist)//逐行讀取
{
//逐行去判斷當前行是空白的.還是注釋的.還是別的.
}
}catch{}
}
自己改改吧.我沒測.
統計JAVA程式碼數
www.onlinedown.net/soft/30001.htm
原始碼行數統計器 1.5
本軟體用於統計軟體工程原始碼行數,可對指定的子目錄下或整個分類樹中所有指定類型的原始碼檔案進行行數統計。
本軟體的統計結果包含原始碼中的注釋行和空行,因為作者認為它們同樣也是原始碼的必要組成部分。
本軟體對 Windows 下和 Unix/Linux 下的原始碼檔案都可以正確地統計行數。