首页 > 学院 > 开发设计 > 正文

java源码之代码统计

2019-11-14 10:14:35
字体:
来源:转载
供稿:网友

源码

import java.io.* ;import java.nio.* ;import java.util.* ;import java.util.regex.* ;import java.nio.channels.FileChannel ;import java.nio.charset.* ;public class CodeStatistics{ //统计代码行 static int codeLine = 0 ; //注释行代码统计 static int annoLine = 0 ; //{}统计 static int backetLine = 0 ; static int searchDirectory( String str ) throws NotFoundFileException { File f = new File(str) ; //判断是路径是否是目录或文件。 if (f.isDirectory()) { return 1 ; }else if (f.isAbsolute()) { return 0 ; } else { throw new NotFoundFileException("没有找到该文件,该目录无效") ; } } static void coderEn( String filePath , int key ) { if ( key == 0 ) { File fileName = new File(filePath) ; if(fileName.getName().matches(".*//.java$")) { Count(filePath) ; } } if ( key == 1 ) { File f1 = new File(filePath) ; File[] downFile = f1.listFiles() ; for (File tmp : downFile) { String f1Name = tmp.getPath() ; int size = searchDirectory(f1Name) ; coderEn( f1Name , size ) ; } } } static void Count( String fileName) { try (BufferedReader br = new BufferedReader( new FileReader(fileName))) { String MuStr = null ; while ((MuStr = br.readLine()) != null) { MuStr = MuStr.trim() ; if (MuStr.startsWith("//") || MuStr.startsWith("*") || MuStr.startsWith("*/") || MuStr.startsWith("/*") || MuStr.equals("")) { annoLine ++ ; } else if (MuStr.startsWith("{") || MuStr.startsWith("}")) { backetLine ++ ; } else { codeLine ++ ; } } } catch (Exception e) { e.PRintStackTrace() ; } } public static void main ( String[] args ) { try(BufferedReader br = new BufferedReader( new InputStreamReader(System.in))) { String IOStr = null ; String filePath = null ; while ((IOStr = br.readLine()) != null ) { filePath = IOStr.replace( '//' , '/') ; coderEn( filePath , searchDirectory( filePath )) ; System.out.println("总行数为:" + ( codeLine + annoLine + backetLine )) ; System.out.println("总代码行数统计:" + codeLine ) ; System.out.println("注释行代码统计:" + annoLine ) ; System.out.println("{}统计:" + backetLine ) ; } } catch ( Exception e ) { e.printStackTrace() ; } }}

自定义异常类

public class NotFoundFileException extends RuntimeException{ public NotFoundFileException() { super() ; } public NotFoundFileException(String message) { super(message) ; }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表