Java练习1_统计代码有效行
2016-01-11 03:57
507 查看
一、统计一个Java文件的有效行数。
1、有效不包括空行
2、不考虑代码见有多行注释的情况
1.读取一个文件使用BufferedReader.
2.
^ 正则开头
正则结尾
[\s&&[^\n]]* 是空格但不是换行,
0个或多个
\n 最后匹配一个空格
^[\s&&[^\n]]*\n 正则结尾
[\s&&[^\n]]* 是空格但不是换行,
0个或多个
\n 最后匹配一个空格
^[\s&&[^\n]]*\n 全行0或多个空格,
并且以换行结束(即空白行)
1、有效不包括空行
2、不考虑代码见有多行注释的情况
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; /** * 代码行数统计 */ public class StatisticCodeLines { public static int normalLines = 0; //有效程序行数 public static int whiteLines = 0; //空白行数 public static int commentLines = 0; //注释行数 public static void main(String[] args) throws IOException{ File file = new File("D://WorkPlace//eclipse//StudioProject//YouthMake//app//src//main//java"); if (file.exists()) { statistic(file); } System.out.println("总有效代码行数: " + normalLines); System.out.println("总空白行数:" + whiteLines); System.out.println("总注释行数:" + commentLines); System.out.println("总行数:" + (normalLines + whiteLines + commentLines)); } private static void statistic(File file) throws IOException { if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { statistic(files[i]); } } if (file.isFile()) { //统计扩展名为java的文件 if(file.getName().matches(".*\\.java")){ parse(file); } } } public static void parse(File file) { BufferedReader br = null; // 判断此行是否为注释行 boolean comment = false; int temp_whiteLines = 0; int temp_commentLines = 0; int temp_normalLines = 0; try { br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { line = line.trim(); if (line.matches("^[//s&&[^//n]]*$")) { // 空行 whiteLines++; temp_whiteLines++; } else if (line.startsWith("/*") && !line.endsWith("*/")) { // 判断此行为"/*"开头的注释行 commentLines++; comment = true; } else if (comment == true && !line.endsWith("*/")) { // 为多行注释中的一行(不是开头和结尾) commentLines++; temp_commentLines++; } else if (comment == true && line.endsWith("*/")) { // 为多行注释的结束行 commentLines++; temp_commentLines++; comment = false; } else if (line.startsWith("//")) { // 单行注释行 commentLines++; temp_commentLines++; } else { // 正常代码行 normalLines++; temp_normalLines++; } } System.out.println("有效行数" + temp_normalLines + " ,空白行数" + temp_whiteLines + " ,注释行数" + temp_commentLines + " ,总行数" + (temp_normalLines + temp_whiteLines + temp_commentLines) + " " + file.getName()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } } }
1.读取一个文件使用BufferedReader.
2.
^ 正则开头
正则结尾
[\s&&[^\n]]* 是空格但不是换行,
0个或多个
\n 最后匹配一个空格
^[\s&&[^\n]]*\n 正则结尾
[\s&&[^\n]]* 是空格但不是换行,
0个或多个
\n 最后匹配一个空格
^[\s&&[^\n]]*\n 全行0或多个空格,
并且以换行结束(即空白行)
相关文章推荐
- java中的i++与++i
- 16Java语法回顾之System.in标准的输入输出流
- Spring3 整合MyBatis3 配置多数据源 动态选择SqlSessionFactory
- Java学习笔记(1)--File类、RandomAccessFile类
- Eclipse 编译StanfordNLP
- Exception in thread main java.lang.UnsopportedClassVersionError解决办法
- [java]Enum在switch中的比较判别
- Java每日解惑
- spring+hibernate的clob大字段处理
- java生成条形码(企业应用)
- java中冒泡排序的实现
- 基于JAVA的图书数据库管理-具有新增,修改,删除,查询功能
- JAVA源码学习-ArrayList
- java泛型--简单泛型
- Spring 通过在classpath自动扫描方式把组件纳入spring容器中管理
- Spring scope属性详解
- Spring 手工装配对象
- Struts工作原理及其优缺点详解
- java 对mysql数据库的基本操作
- java切割字符串将时间2015-12-31 12:12:55.000后面的毫秒删除掉