- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- //***import块实现了字符串的输入***
- public class Code {
- @SuppressWarnings("unused")
- private static final String PROJECT_DIR = "D:\\eclipse"; /**此句有待探讨*/
- //声明文件路径
- private static int totle = 0; //总行数
- private static int source = 0; //代码行数
- private static int blank = 0; //空白行数
- private static int comments = 0; //注释行数
- //private 块实现的是声明了totle;source;blank;comments;这些私有静态的对象
- /**
- * 读取文件夹内java文件
- *
- */
- private static void listNext(File dir) {
- //一个静态方法
- File[] files = dir.listFiles();
- //file 对象定义了文件夹内寻找,作用就是从当前所有文件和文件夹中过滤出所有文件夹
- for (int i = 0; i < files.length; i++) {
- //判断是否是文件夹,如果是文件夹继续向下检索
- if (files[i].isDirectory()) {
- listNext(files[i]);
- } else {
- try {
- if (files[i].getName().endsWith(".java")) {
- System.out.println(files[i].getAbsolutePath());
- javaLine(files[i]);
- }
- }catch (Exception e) { //捕获异常
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 读取java文件总行数,代码行数,空白行数,注释行数
- */
- private static void javaLine(File f) throws FileNotFoundException, IOException{
- //通过调用一个之前定义的对象来表示此文件连接,file参数表示的路径作为参数调用javaLine方法
- String strLine = "";//创建了一个对象。并且加入字符串池中
- String str = fromFile(f);
- //str为引用 "" 是 引用指向的的值,定义一个String 类型的变量str,并为其赋值
- if (str.length() > 0) {
- while (str.indexOf('\n') != -1) {
- //通过一个wile循环体判断这个值,从而做到字符串处理,使得字符串以正确方式显示
- totle++;
- strLine = str.substring(0, str.indexOf('\n')).trim();
- if (strLine.length() == 0 ) {
- blank++;
- }else if (strLine.charAt(0) == '*' || strLine.charAt(0) == '/') {
- comments++;
- }else{
- source++;
- String regEx = "^*//";
- if(regEx(strLine,regEx)){
- comments++;
- }
- }
- str = str.substring(str.indexOf('\n') + 1, str.length());
- //返回给定区间的字符串,以上经过转换后,str能够正常显示
- }
- }
- }
- /**
- * 将java文件以字符数组形式读取
- */
- private static String fromFile(File f) throws FileNotFoundException,IOException {
- FileInputStream fis = new FileInputStream(f);
- // FileInputStream类的重载,读取当前目录下的java文件
- byte[] b = new byte[(int) f.length()];
- //用f的长度初始化一个byte型数组;对返回的长度值做强制类型转换,转换成int型
- fis.read(b);
- fis.close();
- return new String(b);//就是返回一个b的实例
- }
- /**
- * 输入字符串
- * 正则匹配字符串
- */
- private static boolean regEx(String str,String regEx){
- //如果包含正则表达式,则定义特殊的字符,regex匹配的字符串
- Pattern p=Pattern.compile(regEx);//pattern类的实例表示以字符形式指定的正则表达式,此处是一个编译成某种模式
- Matcher m=p.matcher(str);//创建一个匹配器
- boolean result=m.find();
- return result;
- }
- public static void main(String[] args) throws FileNotFoundException, IOException {
- File directory = new File("");//参数为空
- //获取项目路径
- String projectFile = directory.getCanonicalPath() ;
- System.out.println(projectFile+"********");
- listNext(new File(projectFile));
- System.out.println("文件总行数:"+totle+1);
- System.out.println("代码行数:"+source);
- System.out.println("空白行数:"+blank);
- System.out.println("注释行数:"+comments);
- }
- }
源代码计算器
最新推荐文章于 2019-05-26 22:14:09 发布