ツールクラスコード、現在のプロジェクトのすべてのJavaファイルの総数、コードの行の数、コメントの数、および空白行の数を取得します
コードコピーは次のとおりです。
java.io.bufferedreaderをインポートします。
java.io.fileをインポートします。
java.io.fileinputStreamをインポートします。
java.io.filenotfoundexceptionをインポートします。
java.io.filereaderをインポートします。
java.io.ioexceptionをインポートします。
java.io.readerをインポートします。
java.util.regex.matcherをインポートします。
java.util.regex.patternをインポートします。
/**
*
* @author管理者
*
*/
パブリッククラスJavacode {
private static final string project_dir = "c:// test";
private static int totle = 0; //行の合計数
private static int source = 0; //コード行の数
プライベート静的int blank = 0; //空白線の数
private static intコメント= 0; //コメントの数
/**
*フォルダー内のJavaファイルを読み取ります
* @param dir
*/
private static void listnext(file dir){
file [] files = dir.listfiles();
for(int i = 0; i <files.length; i ++){
//フォルダであるかどうかを識別します
if(files [i] .isdirectory()){
listnext(files [i]);
} それ以外 {
試す {
if(files [i] .getname()。endswith( "。java")){
system.out.println(files [i] .getabsolutepath());
Javaline(files [i]);
}
} catch(例外e){
e.printstacktrace();
}
}
}
}
/**
* Javaファイルの行の総数、コードの行の数、空白行の数、コメント行の数を読みます
* @param f
* @throws ioexception
* @throws filenotfoundexception
*/
private static void javaline(file f)throws filenotfoundexception、ioexception {
string strline = "";
string str = fromfile(f);
if(str.length()> 0){
while(str.indexof( '/n')!= -1){
totle ++;
//system.out.println(totle);
strline = str.substring(0、str.indexof( '/n'))。trim();
// str = str.substring(str.indexof( '/n') + 1、str.length());
if(strline.length()== 0){
空白++;
} else if(strline.charat(0)== '*' || strline.charat(0)== '/'){
コメント++;
}それ以外{
ソース++;
string regex = "^*//";
if(regex(strline、regex)){
コメント++;
}
}
str = str.substring(str.indexof( '/n') + 1、str.length());
}
}
}
/**
* Javaファイルを文字配列として読み取ります
* @param f
* @戻る
* @throws filenotfoundexception
* @throws ioexception
*/
filenotfoundexception、ioexception {filenotfoundexceptionをスローするfromfile(ファイルf)からのprivate static string {
fileInputStream fis = new fileInputStream(f);
byte [] b = new byte [(int)f.length()];
fis.read(b);
fis.close();
新しい文字列(b)を返します。
}
/**
*通常の試合
* @param str文字列を入力します
* @param regexレギュラーマッチング文字列
* @戻る
*/
private static boolean regex(string str、string regex){
パターンP = pattern.compile(regex);
Matcher M = P.Matcher(str);
boolean result = m.find();
返品結果;
}
public static void main(string [] args)filenotfoundexception、ioexception {
// file root = new file(project_dir);
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(コメント);
}
}