亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

正則表達式統計怎么統計空行、代碼行、注釋

正則表達式統計怎么統計空行、代碼行、注釋

fenkapian 2016-11-11 19:28:53
用這個程序測代碼行、空行和注釋行,老是顯示都為0行,麻煩看看哪里錯了啊import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class CodeCounter { static long codeLines = 0; static long commentLines = 0; static long whiteLines = 0; public static void main(String[] args) { File f = new File("D:\\Workspaces\\MyEclipse Professional 2014\\DOMTest\\src\\question"); File [] codeFiles = f.listFiles(); for (File file : codeFiles) { if (file.getName().matches(".* \\.java$")) { parse(file); } } System.out.println(codeLines); System.out.println(commentLines); System.out.println(whiteLines); } private static void parse(File f) { BufferedReader br = null; boolean comment = false; try { br = new BufferedReader(new FileReader(f)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[\\s&&[^\\n]]*$")) { whiteLines++; } else if (line.startsWith("/*") && line.endsWith("*/")) { commentLines++; comment = true; } else if (true == comment) { commentLines ++; if (line.endsWith("*/")) { comment = false; } } else { codeLines ++; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } }}
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 1955 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號