android工程代码行数,计算Android项目代码行数

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

只能计算android项目中的java和XML文件,代码实现如下:public class CodeCount {

private static int count = 0;

/**

* 遍历文件

* @param path

*/

private static void listFile(String path){

File file = new File(path);

if(!file.exists()){

System.err.println(file.getAbsolutePath() + "路径不存在");

}

if(file.isDirectory()){

File[] files = file.listFiles();

for(File f:files){

listFile(f.getAbsolutePath());

}

}else{

countLine(file);

}

}

private static void countLine(File file){

if(file.exists()){

if(file.getName().endsWith(".java")){

countLine4Java(file);

}else if(file.getName().endsWith(".xml")){

countLine4Xml(file);

}

}

}

/**

* 计算项目中java文件代码数量

* @param file

*/

private static void countLine4Java(File file){

if(!file.exists() || !file.getName().endsWith(".java")){

return;

}

BufferedReader in = null;

String str = null;

try {

in = new BufferedReader(new FileReader(file));

while((str=in.readLine()) != null){

//去掉tab空隔

str = str.replaceAll("t", "");

//去除行首空格

str = removeSpace(str);

//排除空行

if(str.equals("")){

continue;

}

//排除单行注释

if(str.startsWith("//")){

continue;

}

//排除多行注释

if(str.startsWith("/*") || str.startsWith("*")){

continue;

}

count++;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(in != null){

in.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 计算项目xml文件代码数量

* @param file

*/

private static void countLine4Xml(File file){

if(!file.exists() || !file.getName().endsWith(".xml")){

return;

}

boolean flag = true;

BufferedReader in = null;

String str = null;

try {

in = new BufferedReader(new FileReader(file));

while((str=in.readLine()) != null){

//去掉tab空隔

str = str.replaceAll("t", "");

//去除空格

str = removeSpace(str);

//排除空行

if(str.equals("")){

continue;

}

//排除注释

if(str.startsWith("") || str.endsWith("-->")){

flag = true;

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally{

try {

if(in != null){

in.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

//移除字符串的开始空格

public static String removeSpace(String s){

String str = s;

if(str.equals("") || str == null){

return "";

}

if(!str.startsWith(" ")){

return str;

}

str = str.replaceFirst(" ", "");

return removeSpace(str);

}

public static void main(String[] args) {

long startTime = System.currentTimeMillis();

listFile("D:\workSpace");

long endTime = System.currentTimeMillis();

System.out.println("代码总行数:" + count);

System.out.println("程序运行时间:" + (endTime-startTime));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值