统计文件的代码
来源:互联网 发布:php 图片水印 编辑:程序博客网 时间:2024/05/18 03:01
package com;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class CodeCounter {static long codeLines = 0;static long commentLines = 0;static long spaceLines = 0;static long classnum = 0;static long num = 0;public static void main(String[] args) {File f = new File("F:\\JAVA\\javacode\\GBN.java");//这里的目录要输入你要统计代码的文件夹目录if(f.exists()) {judge(f);}System.out.println("实际代码行数为:"+codeLines);System.out.println("注释代码行数为:"+commentLines);System.out.println("空行行数为:"+spaceLines);num = codeLines+commentLines+spaceLines;System.out.println("总的行数为:"+num);System.out.println("类的数目为:"+classnum);}private static void judge(File f) {if(!f.isFile()) {File[] fs = f.listFiles();//返回的是一个文件数组 是这个文件下面所有的子文件的pathroadfor(File child: fs) { //这是遍历的意思 c++里面也可以这样写judge(child);}}if(f.exists()&&f.isFile()) {//this abstract pathname is a normal filecounter(f);}}private static void counter(File child) {FileReader fr; // read filetry {fr = new FileReader(child);BufferedReader br = new BufferedReader(fr);//把文件里面的内容缓存到br里面String line = "";boolean flag = false;if(child.getName().matches(".*cpp$")||child.getName().matches(".*java$")||child.getName().matches(".*txt$")) { //任意选择文件格式try {while((line=br.readLine())!=null) {String l = line.trim();//去除前导空字符if(l.matches(".*class.+")) {//匹配前后均有0~n个字符的含有class字样的行classnum++;}if(l.matches("^[[\\s]&&[^\\n]]*")) {//保证是空行 spaceLines++;}else if(l.startsWith("/*")&&l.endsWith("*/")) {commentLines++;flag = false;}else if(l.startsWith("/*")) {commentLines++;flag = true;}else if(l.endsWith("*/")) {commentLines++;flag = false;}else if(true == flag) {commentLines++;}else if(l.startsWith("//")) {commentLines++;}else {codeLines++;}}} catch (IOException e) {e.printStackTrace();}}} catch (FileNotFoundException e) {e.printStackTrace();}}}
0 0
- 统计文件的代码
- java代码文件的统计
- 统计代码文件的注释行
- 统计一个项目的代码行数,只统计cpp文件
- Java代码 统计项目的文件数量,和代码行数
- 统计工程的文件数,整体代码的行数
- 正则表达式 统计java文件代码的数量
- 统计文件夹内指定类型文件的代码行数(二)
- 统计某个路径下所有的java文件,以及统计代码数量
- 用代码统计代码文件(python)
- 统计单词的代码
- 统计代码的行数
- 搜索指定类型的代码文件并统计代码的行数
- 统计文件的数量
- 统计文件的行数
- 统计文件的行数
- 统计java代码文件的代码行数(空行、注释不能计数)
- 读取文件的目录结构和统计文件的代码信息
- 调试的错误就是编程给你最好的东西,因为在每个错误上面都标志着前进的一步。
- poj 3237 树链剖分
- 文本分类入门(三)统计学习方法
- iOS开发之理解iOS中的MVC设计模式
- MySQL事务处理
- 统计文件的代码
- 【项目管理】Sonatype Nexus,Maven私服
- Linq To Entity 的增删改查(转)
- 连通图里的深度优先和广度优先遍历
- gene ID转换
- 计算机英语名词解释之软件工程篇
- 架构是一种妥协
- 薪资调研报告
- Core Bluetooth框架之一:Central与Peripheral