รหัสคลาสเครื่องมือรับจำนวนบรรทัดทั้งหมดของไฟล์ 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 Administrator
-
-
JavaCode คลาสสาธารณะ {
สตริงสุดท้ายคงที่ส่วนตัว Project_dir = "C: // test";
ส่วนตัวคงที่ int totle = 0; // จำนวนแถวทั้งหมด
แหล่งข้อมูลคงที่ส่วนตัว = 0; // จำนวนรหัสบรรทัด
int คงที่ private int blank = 0; // จำนวนเส้นเปล่า
ความคิดเห็นแบบคงที่ส่วนตัว = 0; // จำนวนความคิดเห็น
-
* อ่านไฟล์ Java ในโฟลเดอร์
* @param dir
-
Void listNext ส่วนตัว (ไฟล์ DIR) {
ไฟล์ [] files = dir.listfiles ();
สำหรับ (int i = 0; i <files.length; i ++) {
// แยกแยะว่าเป็นโฟลเดอร์หรือไม่
if (ไฟล์ [i] .isdirectory ()) {
ListNext (ไฟล์ [i]);
} อื่น {
พยายาม {
if (ไฟล์ [i] .getName (). endswith (". java")) {
System.out.println (ไฟล์ [i] .getAbsolutepath ());
Javaline (ไฟล์ [i]);
-
} catch (Exception e) {
E.PrintStackTrace ();
-
-
-
-
-
* อ่านจำนวนบรรทัดทั้งหมดของไฟล์ Java จำนวนบรรทัดของรหัสจำนวนบรรทัดว่างจำนวนบรรทัดความคิดเห็น
* @param f
* @throws ioexception
* @throws filenotfoundException
-
โมฆะคงที่ส่วนตัว Javaline (ไฟล์ f) พ่น FilenotFoundException, ioexception {
String strline = "";
string str = fromfile (f);
if (str.length ()> 0) {
ในขณะที่ (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) {
ว่างเปล่า ++;
} อื่นถ้า (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
-
สตริงคงที่ส่วนตัวจากไฟล์ (ไฟล์ f) พ่น filenotfoundexception, ioexception {
FileInputStream FIS = ใหม่ FileInputStream (F);
ไบต์ [] b = ไบต์ใหม่ [(int) f.length ()];
fis.read (b);
fis.close ();
ส่งคืนสตริงใหม่ (b);
-
-
* การจับคู่ปกติ
* @param str enter String
* @param regex สตริงการจับคู่ปกติ
* @กลับ
-
Private Static Boolean Regex (String Str, String regex) {
รูปแบบ p = pattern.compile (regex);
matcher m = p.matcher (str);
ผลลัพธ์บูลีน = m.find ();
ผลการกลับมา;
-
โมฆะคงที่สาธารณะหลัก (String [] args) พ่น filenotfoundexception, ioexception {
// file root = ไฟล์ใหม่ (project_dir);
ไฟล์ไดเรกทอรี = ไฟล์ใหม่ (""); // พารามิเตอร์ว่างเปล่า
// รับเส้นทางโครงการ
String ProjectFile = directory.getCanonicalPath ();
System.out.println (ProjectFile+"=============================================================== =======);
ListNext (ไฟล์ใหม่ (ProjectFile));
System.out.println (totle+1);
System.out.println (แหล่งที่มา);
System.out.println (ว่าง);
System.out.println (ความคิดเห็น);
-
-