您的位置:首页 > 编程语言 > Java开发

java代码统计工具

2015-12-17 22:16 537 查看
很早写的一个java代码统计,现在把代码贴上来,由于完全不会做界面,所以这个就是控制台版的了

很简单的一个类,统计的是.java和.xml(安卓的布局文件也算代码量吧)

如果遇到错误,还希望大家给我留言


package com.io.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class CodeInfo {

int totalLength = 0;
int codeLength = 0;
int infoLength = 0;
int blankLength = 0;

String path;

private ArrayList<String> codePaths;

public CodeInfo(String path) {
this.path = path;
codePaths = new ArrayList<>();
calculate();

}

private void calculate(){
fileSize(new File(path));
for (String string : codePaths) {
countRate(string);
}
}

/**
* 获取注释率
* @return
*/
public float getInfoRate(){
return infoLength/(float)(infoLength+codeLength);
}

/**
* 统计文件夹中文件个数
*
* @param dir
* 文件夹
* @return 文件
*/
private int fileSize(File dir) {
int size = 0;

File[] files = dir.listFiles();

for (File file : files) {
if (file.isFile()) {
size++;
if ((file.getName().endsWith(".java") || file.getName()
.endsWith(".xml"))
&& !file.getName().equals("R.java")
&& !file.getName().equals("BuildConfig.java")) {
codePaths.add(file.getAbsolutePath());
}
} else {
size += fileSize(file);
}
}

return size;
}
/**
* 代码统计
*
* @param path
* 路径
* @return
*/
private float countRate(String path) {
// 注释
int infoLine = 0;
// 代码
int codeLine = 0;
// 空行
int blankLine = 0;
float num = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("/*")) {
while (!(line.trim().endsWith("*/"))) {
infoLine++;
line = reader.readLine();
}
infoLine++;
} else if (line.trim().startsWith("//")) {
infoLine++;
} else if (line.equals("")) {
blankLine++;
} else {
codeLine++;
}
}
/**
*
* sdfsdfsd
*/
} catch (IOException e) {
e.printStackTrace();
}
totalLength = totalLength + infoLine + codeLine + blankLine;
codeLength = codeLength + codeLine;
infoLength = infoLength + infoLine;
blankLength = blankLength + blankLine;
return num;
}

/**
* 统计注释数
*
* @param path
* @return
*/
private int infoCount(String path) {
int count = 0;
try {
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("/*")) {
while (!(line.endsWith("*/"))) {
count++;
line = reader.readLine();
}
count++;
} else if (line.trim().startsWith("//")) {
count++;
}
}
} catch (IOException e) {
e.printStackTrace();
}

return count;
}

/**
* 统计代码数
*
* @param path
* @return
*/
@SuppressWarnings("resource")
private static int codeCount(String path) {
int codeLine = 0;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(path));
String line;

while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("/*")) {
while (!(line.endsWith("*/"))) {
line = reader.readLine();
}
} else if (line.trim().startsWith("//")) {
} else if (line.equals("")) {
} else {
codeLine++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return codeLine;

}

/**
* 统计空行
*
* @param path
* 路径
* @return
*/
private int blankCount(String path) {
int blankCount = 0;
try {
@SuppressWarnings("resource")
BufferedReader reader = new BufferedReader(new FileReader(path));
String line;
while ((line = reader.readLine()) != null) {
if (line.trim().startsWith("/*")) {
while (!(line.endsWith("*/"))) {
line = reader.readLine();
}
} else if (line.trim().startsWith("//")) {
} else if (line.equals("")) {
blankCount++;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return blankCount;
}

/**
* @return path
*/
public String getPath() {
return path;
}

/**
* @param path 要设置的 path
*/
public void setPath(String path) {
this.path = path;
totalLength = 0;
codeLength = 0;
infoLength = 0;
blankLength = 0;
codePaths.clear();
calculate();
}

/**
* @return 总行数
*/
public int getTotalLength() {
return totalLength;
}

/**
* 获取实际行数
* @return
*/
public int getRealLength(){
return totalLength-blankLength;
}

/**
* 获取纯代码行数
* @return codeLength
*/
public int getCodeLength() {
return codeLength;
}

/**
* 获取注释行数
* @return infoLength
*/
public int getInfoLength() {
return infoLength;
}

/**
* 获取代码率
* @return
*/
public float getCodeRate(){
return codeLength/(float)(codeLength+infoLength);
}

/**
* 打印统计信息
*/
public void print(){
System.out.println(path);
System.out.println("共" + getTotalLength() + "行");
System.out.println("实际代码(除去空行)"+getRealLength()+"行");
System.out.println("注释:" + infoLength);
System.out.println("代码:" + codeLength);
System.out.println("空行:" + blankLength);
System.out.println("注释率:" + getInfoRate());
System.out.println("代码率:" + getCodeRate());
}

/**
* @return blankLength
*/
public int getBlankLength() {
return blankLength;
}

}


Main.java
package com.io.util;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入项目文件夹地址:");
String path = scanner.nextLine();
CodeInfo codeInfo = new CodeInfo(path);
codeInfo.print();
}
}


另外打包成了jar 大家可以在控制台使用java -jar使用
点击此处下载jar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息