- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) >
- java如何實(shí)現代碼統計小程序
這篇文章主要介紹了java如何實(shí)現代碼統計小程序,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著(zhù)大家一起了解一下。
具體內容如下
可以測試每周你的工作量
package rexExp; import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException; public class CodeCounter { //三個(gè)靜態(tài)變量存儲行數 static long normalLines = 0; static long commentLines = 0; static long whileLines = 0; public static void main(String[] args) { String pathname = "E:\\testeclipseworkspace\\JavaLearn\\src\\collection"; File file = new File(pathname); File[] codeFiles = file.listFiles();//找到文件夾下面的所有子文件 //文件必須是以.java結尾,用正則表達式來(lái)驗證 for(File child : codeFiles){ if (child.getName().matches(".*\\.java$")) { parse(child); } } System.out.println("normalLines:" + normalLines); System.out.println("commentLines:" + commentLines); System.out.println("whileLines:" + whileLines); } private static void parse(File file) { BufferedReader bReader = null; boolean comment = false; try { bReader = new BufferedReader(new FileReader(file)); //讀其中的每一行 String line = ""; while((line=bReader.readLine()) != null){ line = line.trim();//去掉首尾空格 //統計空行的行數 if (line.matches("^[\\s&&[^\\n]]*$")) { whileLines++; } //統計注釋的行數 else if (line.startsWith("/*") && !line.endsWith("*/")) { commentLines++; //如果遇到"/*",說(shuō)明注釋開(kāi)始了 comment = true; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; } else if (true == comment) { commentLines++; if (line.endsWith("*/")) { comment = false; } } else if(line.startsWith("//")){ commentLines++; } else { normalLines++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (bReader != null) { try { bReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站