zoukankan      html  css  js  c++  java
  • java代码行数计算器

     

     

    import java.io.BufferedReader; 
    import java.io.File; 
    import java.io.FileReader; 
    import java.util.ArrayList; 
    import java.util.List; 
      
    /** 
     * ********************************************** 
     * @description 计算源代码(src)行数,不计算空行 
     *     宗旨:将src下所有文件组装成list,再筛选出文件,对文件进行遍历读取 
      * @version 1.0 
     *********************************************** 
     */
    public class LineCounter { 
        List<File> list = new ArrayList<File>(); 
        int linenumber = 0; 
          
        FileReader fr = null; 
        BufferedReader br = null; 
      
        public void counter(String projectName) { 
    //        String path = System.getProperty("user.dir"); 
            String path = LineCounter.class.getResource("/").getPath();  // 同下个path 
            path = path.substring(0, path.length() - 24) + projectName + "/src"; 
            System.out.println(path); 
            File file = new File(path); 
            File files[] = null; 
            files = file.listFiles(); 
            addFile(files); 
            isDirectory(files); 
            readLinePerFile(); 
            System.out.println("Totle:" + linenumber + "行"); 
        } 
      
        // 判断是否是目录 
        public void isDirectory(File[] files) { 
            for (File s : files) { 
                if (s.isDirectory()) { 
                    File file[] = s.listFiles(); 
                    addFile(file); 
                    isDirectory(file); 
                    continue; 
                } 
            } 
        } 
      
        //将src下所有文件组织成list 
        public void addFile(File file[]) { 
            for (int index = 0; index < file.length; index++) { 
                list.add(file[index]); 
                // System.out.println(list.size()); 
            } 
        } 
          
        //读取非空白行 
        public void readLinePerFile() { 
            try { 
                for (File s : list) { 
                    int yuan = linenumber; 
                    if (s.isDirectory()) { 
                        continue; 
                    } 
                    fr = new FileReader(s); 
                    br = new BufferedReader(fr); 
                    String i = ""; 
                    while ((i = br.readLine()) != null) { 
                        if (isBlankLine(i)) 
                            linenumber++; 
                    } 
                    System.out.print(s.getName()); 
                    System.out.println("		有" + (linenumber - yuan) + "行"); 
                } 
            } catch (Exception e) { 
                e.printStackTrace(); 
            } finally { 
                if (br != null) { 
                    try { 
                        br.close(); 
                    } catch (Exception e) { 
                    } 
                } 
                if (fr != null) { 
                    try { 
                        fr.close(); 
                    } catch (Exception e) { 
                    } 
                } 
            } 
        } 
      
        //是否是空行 
        public boolean isBlankLine(String i) { 
            if (i.trim().length() == 0) { 
                return false; 
            } else { 
                return true; 
            } 
        } 
          
        public static void main(String args[]) { 
            LineCounter lc = new LineCounter(); 
            String projectName = "";     //这里传入你的项目名称 
            lc.counter(projectName); 
        } 
    }
  • 相关阅读:
    10gen发布MongoDB增量备份服务
    JSON.NET 5中的架构变更
    Snowbox 2.0 发布,POP3 邮件服务器
    资源监控工具 glances
    Druid 0.2.18 发布,阿里巴巴数据库连接池
    Groovy 更新到 2.0.8 and 2.1.3
    Apache Libcloud 0.12.4 发布,统一云计算接口
    Go1.1性能测试报告(和C差距在10%以内)
    Apache Camel 2.11.0 发布,规则引擎
    2010年01月01日0时0分 总结我的2009
  • 原文地址:https://www.cnblogs.com/yejiurui/p/3257002.html
Copyright © 2011-2022 走看看