欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java 代码统计(简陋版本),java代码统计,一个小的java代码统计

来源: javaer 分享于  点击 36566 次 点评:252

java 代码统计(简陋版本),java代码统计,一个小的java代码统计


一个小的java代码统计。很多情况没有处理 比如 空行 处理 欢迎指出缺点。

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class CalCodeLine {    /**     * @author silencer     * @throws IOException     */    public static void main(String[] args) throws IOException {        String filePath ="";        File file = new File(filePath);        System.out.println(" all code line :"+getFileLine(file));    }    /**     *     * @param file     * @return  fileLine     * @throws IOException     */    public static int getFileLine(File file) throws IOException {        int sFile = 0;//http://www.huiyi8.com/jiaoben/        if (file.isDirectory()) {            File[] files = file.listFiles();            for (File tempFile : files) {                if (tempFile.isDirectory()) {                    sFile += getFileLine(tempFile);                } else {                    sFile += getJavaTextLength(tempFile);                }            }        }else{            sFile += getJavaTextLength(file);        }        return sFile;    }    /**     *     * @param file     * @return textLine     * @throws IOException     */    public static int getJavaTextLength(File file) throws IOException {        int sLength = 0;        if (file != null && file.getName().endsWith(".java")) {            BufferedReader br = new BufferedReader(new FileReader(file));            StringBuffer sb = new StringBuffer();            sb.append(br.readLine());            boolean isComment = false;            boolean isSigleComment = false;            while (!sb.toString().equals("null")) {                if (sb.toString().startsWith("//")) {                    isSigleComment = true;                } else if (sb.toString().contains("/*")) {                    isComment = true;                } else if (sb.toString().contains("*/")) {                    isComment = false;                    sLength -= 1;                }                if (!isSigleComment && !isComment                        && !sb.toString().startsWith("import")                        && !sb.toString().isEmpty()) {                    sLength += 1;                }                isSigleComment = false;                sb = sb.delete(0, sb.length());                sb.append(br.readLine());            }            br.close();        }        return sLength;    }}
相关栏目:

用户点评