Código da classe de ferramentas, obtenha o número total de linhas de todos os arquivos Java no projeto atual, o número de linhas de código, o número de comentários e o número de linhas em branco
A cópia do código é a seguinte:
importar java.io.bufferedReader;
importar java.io.file;
importar java.io.fileInputStream;
importar java.io.filenotfoundException;
importar java.io.fileReader;
importar java.io.ioException;
importar java.io.Reader;
importar java.util.regex.matcher;
importar java.util.regex.pattern;
/**
*
* Administrador @Author
*
*/
classe pública Javacode {
String final estática privada Project_dir = "c: // teste";
private estático int tot = 0; // Número total de linhas
private static int fonte = 0; // número de linhas de código
estático privado int em branco = 0; // Número de linhas em branco
Comentários privados estáticos int = 0; // Número de comentários
/**
* Leia os arquivos java na pasta
* @param dir
*/
Private Static Void ListNext (arquivo de arquivo) {
Arquivo [] arquivos = dir.ListFiles ();
for (int i = 0; i <files.length; i ++) {
// discernir, seja uma pasta.
if (arquivos [i] .isdirectory ()) {
listNext (arquivos [i]);
} outro {
tentar {
if (arquivos [i] .getName (). endswith (". java")) {
System.out.println (arquivos [i] .getabsolutepath ());
javaline (arquivos [i]);
}
} catch (Exceção e) {
E.PrintStackTrace ();
}
}
}
}
/**
* Leia o número total de linhas de arquivo java, número de linhas de código, número de linhas em branco, número de linhas de comentários
* @param f
* @THOWSows IoException
* @Throws filenotfoundException
*/
private estático void javaline (arquivo f) lança fileNotfoundException, ioexception {
String strline = "";
String str = do arquivo (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) {
em branco ++;
} else if (strline.charat (0) == '*' || strline.charat (0) == '/') {
Comentários ++;
}outro{
fonte ++;
String regex = "^*//";
if (regex (strline, regex)) {
Comentários ++;
}
}
str = str.substring (str.indexOF ('/n') + 1, str.Length ());
}
}
}
/**
* Leia o arquivo java como uma matriz de caracteres
* @param f
* @retornar
* @Throws filenotfoundException
* @THOWSows IoException
*/
string estática privada do arquivo (arquivo f) lança fileNotfoundException, ioexception {
FileInputStream fis = new FileInputStream (f);
byte [] b = novo byte [(int) f.Length ()];
fis.read (b);
fis.close ();
retornar nova string (b);
}
/**
* Combinação regular
* @param str enter string
* @param regex string de correspondência regular
* @retornar
*/
private estático booleano regex (string str, string regex) {
Padrão p = padrão.compile (regex);
Matcher M = P.Matcher (STR);
resultado booleano = m.find ();
resultado de retorno;
}
public static void main (string [] args) lança fileNotfoundException, ioexception {
// arquivo root = novo arquivo (Project_dir);
Diretório de arquivos = novo arquivo (""); // o parâmetro está vazio
// Obtenha o caminho do projeto
String ProjectFile = Directory.getCanonicalPath ();
System.out.println (ProjectFile+"============================================ =======);
listNext (novo arquivo (ProjectFile));
System.out.println (totle+1);
System.out.println (fonte);
System.out.println (em branco);
System.out.println (Comentários);
}
}