zoukankan      html  css  js  c++  java
  • java 计算源码的行数

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;


    public class GetSourceCodeSumLine {
        /**
         * @param args
         */
        public static void main(String[] args) {
    // 方法1
            String JavaSourceCodeDir[] = { "E:\...\src", "G:\...\src" };
            long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDir);


    // 方法2
    //        ArrayList<String> JavaSourceCodeDirs = new ArrayList<String>();
    //        JavaSourceCodeDirs.add("G:\workspace\...\src");
    //        JavaSourceCodeDirs.add("G:\workspace\...\src");
    //        long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDirs);

            System.out.println(JavaSourceCodeDir.toString() + ":共有 "
                    + codeCountLine + " 行java源代码");
        }
        
        
        public static long getJavaSourceCodeCountLine(String[] JavaSourceCodeDirs) {
            long codeCountLine = 0;
            for (String dirPath : JavaSourceCodeDirs) {
                File fl = new File(dirPath);
                if (fl.exists()) {
                    codeCountLine += getJavaFileCountLine(fl);
                }
            }
            return codeCountLine;
        }
        
        public static long getJavaSourceCodeCountLine(ArrayList<String> JavaSourceCodeDirs) {
            long codeCountLine = 0;
            for (String dirPath : JavaSourceCodeDirs) {
                File fl = new File(dirPath);
                if (fl.exists()) {
                    codeCountLine += getJavaFileCountLine(fl);
                }
            }
            return codeCountLine;
        }

        private static long getJavaFileCountLine(File fl) {
            long javaCodeCountLine = 0;

            if (fl != null && fl.exists()) {
                if (fl.isDirectory()) {
                    File[] listFiles = fl.listFiles();
                    for (File file : listFiles) {
                        javaCodeCountLine += getJavaFileCountLine(file);
                    }
                } else if (fl.isFile() && !fl.isHidden()) {

          // 过滤源码的后缀坠
                    if (fl.getName().endsWith(".java")) {

                        FileReader fr = null;
                        BufferedReader br = null;
                        try {
                            fr = new FileReader(fl);
                            br = new BufferedReader(fr);
                            while (br.readLine() != null)
                                javaCodeCountLine++;
                            br.close();
                            fr.close();
                        } catch (Exception e) {
                        } finally {
                            if (fr != null) {
                                try {
                                    fr.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } finally {
                                    fr = null;
                                }
                            }
                            if (br != null) {
                                try {
                                    br.close();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                } finally {
                                    br = null;
                                }
                            }
                        }
                    }

                }
            }
            return javaCodeCountLine;
        }
    }

  • 相关阅读:
    【python-opencv】opencv基础操作之一
    【胎教】做AI的基础,开始学习。
    【实习】博士生找实习的囧事之其一
    【经验】CS
    【keras】用tensorboard监视CNN每一层的输出
    【算法】背包九讲
    【计算机网络】大数据 云计算 人工智能
    【算法】shortest distance
    【git】git hello world
    【算法】深度优先 马走日 Hamilton routes
  • 原文地址:https://www.cnblogs.com/wf-l5201314/p/6381519.html
Copyright © 2011-2022 走看看