”4635“――這三個月以來的成就。
來源:程序員人生 發布時間:2014-09-08 19:25:59 閱讀次數:2955次
今天晚上突發奇想,想算一下到新公司三個月以來所寫的代碼量。上網找了一下沒有現成的解決方案。最后找了大牛同事,同事也沒做過這個事情,不過算是找到了解決方案:利用shell去查找,不過速度真心夠慢的。最后還是自己寫了一個java代碼去計算自己的代碼量。
計算行數代碼如下:
public static long totalCount;
//rootFile是根文件夾
public void readJava(String author,File rootFile) {
File[] files = rootFile.listFiles();
for (File file : files) {
if (file.isFile() && (file.getName().endsWith("java") || file.getName().endsWith("js"))) {
try {
getCount(author, file);
} catch (IOException e) {
e.printStackTrace();
}
}
if (file.isDirectory()) {
readJava(author,file);
}
}
}
public long getCount(String author, File file) throws IOException {
boolean belongToAuthorFlag = false;
long count = 0;
FileReader fileReader = new FileReader(file);
BufferedReader reader = new BufferedReader(fileReader);
String content = "";
while ((content = reader.readLine()) != null) {
count++;
if (!belongToAuthorFlag && content.contains(author)) {
belongToAuthorFlag = true;
}
}
reader.close();
if (belongToAuthorFlag) {
totalCount += count;
System.out.println(count + "---" + file.getName());
}
return totalCount;
}
public static void main(String[] args) {
CountCodeUtil countCode = new CountCodeUtil();
String rootPath = "/home/liubin/workspace/tz";
File rootFile = new File(rootPath);
System.out.println("startTime:" + new Date());
countCode.readJava("dingguangxian",rootFile);
System.out.println("startTime:" + new Date());
System.out.println("totalCount:" + totalCount);
}
這個代碼還是有些缺點的,可以不用讀取整個文件的,如果讀到public class還沒有找到符合的字符串就可以換下一個文件。然后就是沒有將空換行除去和一半的花括弧‘}’去除。
不過還是將就能用的。
我自己的結果就是4635行,同事也是比較驚訝,竟然能寫這么多。畢竟大牛同事在公司有兩年了,代碼量是2.5W行。當然了行數不能代表什么,我的代碼質量當然不能和大牛比了。但這個也算是我正式工作以來的成績吧。后面還得繼續努力。提高代碼的質量
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈