您的位置:首页 > 编程语言 > Java开发

利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)

2008-10-15 15:36 836 查看
这是一个利用正则表达式获取特定文件(如java class)并对其进行处理(如代码统计)的程序。可以在特定的指定的文件目录下寻找指定的文件,在下面程序里主要是寻找java文件并进行数量统计和代码统计(代码行、空行、注释行)。至于删除那些class文件主要是因为我统计的是我下载的JDK的源码,里面的class文件不知道什么时候产生的,手动删除实在麻烦。在我的机器上的结果是:StatisticalJavaFiles:7070
                        CodeLine:894658
                        ComentLine:901352
                        WhiteLine :238595       

呵呵,Sun公司发布的Java源码有90万行,不算多。估计没公布的比公布的还多。而且统计出来的结果中注释的语句比代码行还多。这也充分说明了注释得重要性。

/*

 * StatisticalCodes.java 2008-10-15

 * This program was write for find out some kinds of 

 * file that we specify.

 */

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

/**

 * @author Sancho_lai

 */

public class StatisticalCodes  {

    

    //codes line count

    private static long codeLines  = 0;

    //comment line count

    private static long commentLines = 0;

    //white line count

    private static long whiteLines   = 0;

    //number of java files was found

    private static long statisticalJavaFiles = 0; 

    

    //input a directory that you want,but it will cost a NullPointException when you input a <root directory>

    private static String path = "D://jdkSrc";

    

    public static void main(String[] args) {

        File file = new File(path);

        

        findSpecifiedDoc(file);

        

        System.out.println("StatisticalJavaFiles:" + statisticalJavaFiles);

        System.out.println("CodeLine:" + codeLines);

        System.out.println("ComentLine:" + commentLines);

        System.out.println("WhiteLine :" + whiteLines);

        

    }

    /**

     * find all the files that fit for the Regular Expression you specify

     * and then deal with it and count the lines and delete the class files

     * you can extend it you want to be

     */

    private static void findSpecifiedDoc(File file) {

        File[] fileArray = file.listFiles();

        for(File child:fileArray) {

            if(child.isDirectory()) { //when child was a directory recursively call <findSpecifiedDoc> until get java file 

                findSpecifiedDoc(child);

            }else if(child.getName().matches(".*//.java$")) { //when the <child> was a java file ,count the lines

                parser(child);

                statisticalJavaFiles++;

            }else if(child.getName().matches(".*//.class$")){ //when the <child> was a class file , delete it

                deleteClassFile(child);

            }

        } 

    }

    /**

     * delete the class type file

     * @param child

     */

    private static void deleteClassFile(File child) {

        child.delete();

    }

    /**

     * parser the java file count the codeLines ...

     * using the regular expression for judging

     * @param child

     */

    private static void parser(File child) {

        BufferedReader br = null;

        boolean comment = false;

        String line = "";

        try {

            br = new BufferedReader(new FileReader(child));

            while((line = br.readLine()) != null) {

                line = line.trim();

                if(line.matches("^[//s&&[^//n]]*$")) { //

                    whiteLines ++;

                } else if (line.startsWith("/*") && !line.endsWith("*/")) {//comment lines start with /* 

                    commentLines ++;

                    comment = true; 

                } else if (line.startsWith("/*") && line.endsWith("*/")) {

                    commentLines ++;

                } else if (true == comment) {

                    commentLines ++;

                    if(line.endsWith("*/")) {

                        comment = false;

                    }

                } else if (line.startsWith("//")) {

                    commentLines ++;

                } else { //it suppose that the java files just contain three kinds of lines

                    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();

            }

          }

        }

    }   

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐