代码统计——过滤注释空行
2016-08-08 16:15
162 查看
通常我们自己想看看自己写了多少行代码——虽然这样很冠冕堂皇,废话少说了,直接上代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by Kuring on 2016/8/8.
*/
public class CountRows {
private long rows = 0L;
private long chars = 0L;
private long fileNum = 0L;
private String fileType = "java";
public static void main(String[] args) throws IOException {
String path = args[0];
String fileType = args[1];
CountRows countRows = new CountRows();
countRows.setFileType(fileType);
System.out.println("开始计算目录 " + path + " 下文件的总行数...");
countRows.countRows(path);
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public void countRows(String path) throws IOException {
File file = new File(path);
getAllFile(file);
System.out.println("统计文件数:" + this.fileNum);
System.out.println("总字符数:" + this.chars + "个");
System.out.println("总行数为:" + this.rows + " 行");
}
private void getAllFile(File file) throws IOException {
File[] files = file.listFiles();
for (File tmp : files
) {
String fileName = tmp.getName();
if (tmp.isFile() && fileName.endsWith(this.fileType)) {
long chars = tmp.length();
//System.out.println("文件名称:" + fileName);
//System.out.println("文件字符数:" + chars);
BufferedReader bufferedReader = new BufferedReader(new FileReader(tmp));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
//按照编码规范剔除空行和注释行
//if (!line.trim().equals("")) {
// if(!line.trim().startsWith("/**") &&
// !line.trim().startsWith("*") &&
// !line.trim().startsWith("*/") &&
// !line.trim().startsWith("//")) {
// this.rows++;
// }
//}
this.rows++;
}
this.chars += chars;
this.fileNum++;
} else if (tmp.isDirectory()) {
getAllFile(tmp);
}
}
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by Kuring on 2016/8/8.
*/
public class CountRows {
private long rows = 0L;
private long chars = 0L;
private long fileNum = 0L;
private String fileType = "java";
public static void main(String[] args) throws IOException {
String path = args[0];
String fileType = args[1];
CountRows countRows = new CountRows();
countRows.setFileType(fileType);
System.out.println("开始计算目录 " + path + " 下文件的总行数...");
countRows.countRows(path);
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
public void countRows(String path) throws IOException {
File file = new File(path);
getAllFile(file);
System.out.println("统计文件数:" + this.fileNum);
System.out.println("总字符数:" + this.chars + "个");
System.out.println("总行数为:" + this.rows + " 行");
}
private void getAllFile(File file) throws IOException {
File[] files = file.listFiles();
for (File tmp : files
) {
String fileName = tmp.getName();
if (tmp.isFile() && fileName.endsWith(this.fileType)) {
long chars = tmp.length();
//System.out.println("文件名称:" + fileName);
//System.out.println("文件字符数:" + chars);
BufferedReader bufferedReader = new BufferedReader(new FileReader(tmp));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
//按照编码规范剔除空行和注释行
//if (!line.trim().equals("")) {
// if(!line.trim().startsWith("/**") &&
// !line.trim().startsWith("*") &&
// !line.trim().startsWith("*/") &&
// !line.trim().startsWith("//")) {
// this.rows++;
// }
//}
this.rows++;
}
this.chars += chars;
this.fileNum++;
} else if (tmp.isDirectory()) {
getAllFile(tmp);
}
}
}
}
相关文章推荐
- 统计java代码行数的代码(除注释,空行)
- 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- Python Show-Me-the-Code 第 0007 题 统计代码行数(注释,空行,总行数)小程序
- JAVA统计代码行数的简单代码,很准确,去掉注释,空行等
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- python统计代码空行与注释
- 用python统计代码行(包括空行和注释)
- Python实现代码统计(代码+注释+空行)
- 利用正则表达式实现统计代码中的注释行、空行、以及代码行的数量。
- python写的统计PYTHON文件的(代码行数,空行数,注释行数)小程序
- python 统计一个目录下代码的行数、空行和注释
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- iOS代码统计(空行、注释、代码)
- 用python统计代码行的示例(包括空行和注释)
- (java 多线程)统计代码行数、注释行数、空行数
- 第 0007 题:有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。
- 统计java代码文件的代码行数(空行、注释不能计数)
- PHP统计目录下的文件总数及代码行数(去除注释及空行)
- 每天一个python小程序 007有个目录,里面是你自己写过的程序,统计一下你写过多少行代码。包括空行和注释,但是要分别列出来。