Java 编写程序统计项目文件总数(可指定后缀名)、文件行数(可去除空白行和注释)

发布时间:2018-09-19作者:laosun阅读(1835)

Java

    Java 编写程序统计项目文件总数(可指定后缀名)、文件行数(可去除空白行和注释)

    直接看源码吧,没什么好解释的。

    package com.sunjs.demo;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Java代码统计 [指定后缀的文件总数、文件内行数、文件内空白行数、文件内注释行数]
     * @author sun
     */
    public class CodeCounterDemo {
    
    	private static Integer codeLine = 0;
    	private static Integer commentsLine = 0;
    	private static Integer blankLine = 0;
    	private static Integer fileCount = 0;
    	
    	private static final String PATH = "/Users/sun/Documents/workspace/xxx";//检索目录
    	private static final List<String> suffixList = new ArrayList<>();//需要统计的文件名后缀
    
    	static{
    		suffixList.add("java");
    //		suffixList.add("xml");
    	}
    	
    	public static void main(String[] args) {
    		long s = System.currentTimeMillis();
    		File file = new File(PATH);
    		getAllFile(file);
    		System.out.println("文件总数:" + fileCount);
    		System.out.println("代码行数:" + codeLine);
    		System.out.println("空白行数:" + blankLine);
    		System.out.println("注释行数:" + commentsLine);
    		System.out.println("总行数:" + (codeLine + blankLine + commentsLine));
    		long e = System.currentTimeMillis();
    		System.out.println("\n总耗时:"+(e-s));
    	}
    
    	public static void getAllFile(File file) {
    		BufferedReader br = null;
    		String s = null;
    
    		if (file.isDirectory()) {
    			File[] files = file.listFiles();
    			for (File f : files) {
    				getAllFile(f);
    			}
    		} else {
    			if(file.getName().indexOf(".")!=-1){
    //				if(file.getName().equals("pom.xml")){
    //					System.out.println();
    //				}
    //				System.out.println(file.getName());
    				String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
    				if(suffixList.contains(suffix)){
    					fileCount++;
    					try {
    						br = new BufferedReader(new FileReader(file));
    						boolean comm = false;
    						while ((s = br.readLine()) != null) {
    							if (s.startsWith("/*") && s.endsWith("*/")) {
    								commentsLine++;
    							} else if (s.trim().startsWith("//")) {
    								commentsLine++;
    							} else if (s.startsWith("/*") && !s.endsWith("*/")) {
    								commentsLine++;
    								comm = true;
    							} else if (!s.startsWith("/*") && s.endsWith("*/")) {
    								commentsLine++;
    								comm = false;
    							} else if (comm) {
    								commentsLine++;
    							} else if (s.trim().length() < 1) {
    								blankLine++;
    							} else {
    								codeLine++;
    							}
    						}
    						br.close();
    					} catch (FileNotFoundException e) {
    						e.printStackTrace();
    					} catch (IOException e) {
    						e.printStackTrace();
    					}
    				}
    			}
    		}
    	}
    }

    输出结果:

    文件总数:2340
    代码行数:745719
    空白行数:143001
    注释行数:49551
    总行数:938271
    
    总耗时:1369


3 +1

版权声明

 Java  源码

 请文明留言

0 条评论