Kode alat-alat, dapatkan jumlah total baris semua file java dalam proyek saat ini, jumlah baris kode, jumlah komentar, dan jumlah baris kosong
Salinan kode adalah sebagai berikut:
impor java.io.bufferedReader;
impor java.io.file;
impor java.io.fileInputStream;
impor java.io.filenotfoundException;
impor java.io.filereader;
impor java.io.ioException;
impor java.io.reader;
impor java.util.regex.matcher;
impor java.util.regex.pattern;
/**
*
* Administrator @Author
*
*/
JavaCode kelas publik {
Private Static Final String Project_dir = "c: // test";
private static int totle = 0; // Jumlah total baris
Sumber int statis pribadi = 0; // Jumlah baris kode
private static int blank = 0; // Jumlah garis kosong
Komentar int private static = 0; // Jumlah komentar
/**
* Baca file java di folder
* @param dir
*/
private static void listNext (file dir) {
File [] file = dir.listfiles ();
untuk (int i = 0; i <file.length; i ++) {
// Beri tahu apakah itu folder.
if (file [i] .isDirectory ()) {
listNext (file [i]);
} kalau tidak {
mencoba {
if (file [i] .getName (). endswith (". java")) {
System.out.println (file [i] .getAbsolutePath ());
Javaline (file [i]);
}
} catch (Exception e) {
e.printstacktrace ();
}
}
}
}
/**
* Baca jumlah total baris file java, jumlah baris kode, jumlah baris kosong, jumlah baris komentar
* @param f
* @Throws IoException
* @Throws FileNotFoundException
*/
private static void javaline (file f) melempar 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) {
blank ++;
} else if (strline.charat (0) == '*' || strline.charat (0) == '/') {
komentar ++;
}kalau tidak{
sumber ++;
String regex = "^*//";
if (regex (strline, regex)) {
komentar ++;
}
}
str = str.substring (str.indexOf ('/n') + 1, str.length ());
}
}
}
/**
* Baca file java sebagai array karakter
* @param f
* @kembali
* @Throws FileNotFoundException
* @Throws IoException
*/
string statis pribadi dariFile (file f) melempar FileNotFoundException, IoException {
FileInputStream fis = FileInputStream baru (f);
byte [] b = byte baru [(int) f.length ()];
fis.read (b);
fis.close ();
mengembalikan string baru (b);
}
/**
* Pertandingan reguler
* @param str enter string
* @param regex string reguler pencocokan
* @kembali
*/
private static boolean regex (string str string, string regex) {
Pola p = pola.compile (regex);
Pencocokan m = p.matcher (str);
hasil boolean = m.find ();
hasil pengembalian;
}
public static void main (string [] args) melempar filenotfoundException, ioException {
// file root = file baru (proyek_dir);
Direktori File = File Baru (""); // Parameternya kosong
// Dapatkan jalur proyek
String ProjectFile = Directory.getCanonicalPath ();
System.out.println (ProjectFile+"============================================ =======);
listNext (file baru (ProjectFile));
System.out.println (totle+1);
System.out.println (sumber);
System.out.println (kosong);
System.out.println (komentar);
}
}