zoukankan      html  css  js  c++  java
  • java简单统计.java文件中的有效代码行,空行,注释行

    package regxdemo;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class CountFile {
    
        /**
         * @param args
         */
        static int cntCode=0, cntNode=0, cntSpace=0;
        static boolean flagNode = false;
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            BufferedReader br = null;
            try {
                br = new BufferedReader(new FileReader("TestFile.java"));
                String line=null;
                while((line = br.readLine()) != null)
                    pattern(line);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            System.out.println("注释行: " + cntNode);
            System.out.println("空行: " + cntSpace);
            System.out.println("代码行: " + cntCode);
            System.out.println("总行: " + (cntNode+cntSpace+cntCode));
            
        }
    
        private static void pattern(String line) {
            // TODO Auto-generated method stub
            String regxNodeBegin = "\s*/\*.*";
            String regxNodeEnd = ".*\*/\s*";
            String regx = "//.*";
            String regxSpace = "\s*";
            if(line.matches(regxNodeBegin) && line.matches(regxNodeEnd)){
                ++cntNode;
                return ;
            }
            if(line.matches(regxNodeBegin)){
                ++cntNode;
                flagNode = true;
            } else if(line.matches(regxNodeEnd)){
                ++cntNode;
                flagNode = false;
            } else if(line.matches(regxSpace))
                 ++cntSpace;
            else if(line.matches(regx))
                 ++cntNode;
            else if(flagNode)
                 ++cntNode;
            else ++cntCode;
        }
    
    }
  • 相关阅读:
    dir for RequestHandler and request
    python globals和locals
    Spring AOP(通知、连接点、切点、切面)
    Elasticsearch和Head插件安装(转)
    服务发现
    全面的软件测试( 转)
    软件开发项目人员配置
    阿里云oss缩略图如何产生读取 超简单 不看后悔(转)
    Elasticsearch模糊查询
    小米Pro 安装苹果系统
  • 原文地址:https://www.cnblogs.com/hujunzheng/p/4072518.html
Copyright © 2011-2022 走看看