为了账号安全,请及时绑定邮箱和手机立即绑定

正则表达式统计怎么统计空行、代码行、注释

正则表达式统计怎么统计空行、代码行、注释

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 关注
  • 1939 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信