도구 클래스 코드, 현재 프로젝트에서 모든 Java 파일의 총 줄 수, 코드 줄 수, 댓글 수 및 빈 줄 수를 얻습니다.
코드 사본은 다음과 같습니다.
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;
java.util.regex.pattern import;
/**
*
* @Author 관리자
*
*/
공개 클래스 javacode {
개인 정적 최종 문자열 project_dir = "C : // test";
개인 정적 int totle = 0; // 총 행 수
비공개 정적 int 소스 = 0; // 코드 라인 수
비공개 정적 int blank = 0; // 빈 줄의 수
개인 정적 int 댓글 = 0; // 주석 수
/**
* 폴더에서 Java 파일을 읽습니다
* @param dir
*/
개인 정적 void listnext (파일 dir) {
file [] files = dir.listfiles ();
for (int i = 0; i <files.length; i ++) {
// 폴더인지 여부를 식별하십시오
if (files [i] .isdirectory ()) {
listNext (파일 [i]);
} 또 다른 {
노력하다 {
if (files [i] .getName (). EndSwith ( ". java")) {
System.out.println (파일 [i] .getabsolutepath ());
Javaline (파일 [i]);
}
} catch (예외 e) {
e.printstacktrace ();
}
}
}
}
/**
* Java 파일의 총 라인 수, 코드 줄 수, 빈 줄 수, 댓글 줄 수 읽기
* @param f
* @throws ioexception
* @throws filenotfoundException
*/
개인 정적 무효 Javaline (파일 f)은 filenotfoundException, ioException {
문자열 strline = "";
문자열 str = fromfile (f);
if (str.length ()> 0) {
while (str.indexof ( '/n')! = -1) {
토틀 ++;
//system.out.println(Totle);
strine = str.substring (0, str.indexof ( '/n')). trim ();
// str = str.substring (str.indexof ( '/n') + 1, str.length ());
if (strine.length () == 0) {
blank ++;
} else if (strline.charat (0) == '*'|| Strline.charat (0) == '/') {
의견 ++;
}또 다른{
소스 ++;
String regex = "^*//";
if (regex (strine, regex)) {
의견 ++;
}
}
str = str.substring (str.indexof ( '/n') + 1, str.length ());
}
}
}
/**
* Java 파일을 문자 배열로 읽으십시오
* @param f
* @반품
* @throws filenotfoundException
* @throws ioexception
*/
fromfile (파일 f) 프라이버시 정적 문자열 filenotfoundexception, ioexception {
fileInputStream fis = 새로운 fileInputStream (f);
바이트 [] b = 새로운 바이트 [(int) f.length ()];
fis.read (b);
fis.close ();
새 문자열 (b)을 반환합니다.
}
/**
* 정규 경기
* @param str string을 입력합니다
* @param regex 정규 매칭 문자열
* @반품
*/
Private Static Boolean Regex (String Str, String Regex) {
Pattern P = Pattern.compile (Regex);
매치 자 m = p.matcher (str);
부울 결과 = M.find ();
반환 결과;
}
public static void main (String [] args)은 filenotfoundException, ioException {
// 파일 루트 = 새 파일 (project_dir);
파일 디렉토리 = 새 파일 ( ""); // 매개 변수가 비어 있습니다
// 프로젝트 경로를 얻습니다
문자열 projectFile = directory.getCanonicalPath ();
System.out.println (ProjectFile+"========================================== =======);
ListNext (새 파일 (projectFile));
System.out.println (Totle+1);
System.out.println (소스);
System.out.println (blank);
System.out.println (댓글);
}
}