package cn.itcast.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CodeNumber {
/**
* src : 放代码的路径
*/
public static long num = 0;
public static void main(String[] args) {
String src = "F://我的笔记//代码";
File srcFile = new File(src);
try {
find(srcFile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("你已经敲了"+num+"行代码!");
}
/**
* 判断,扩展名是否符合条件
*/
public static boolean isCode(String fileName) {
boolean flag = false;
if (fileName == null) {
return false;
}
if (fileName.endsWith(".java") || fileName.endsWith(".jsp")
|| fileName.endsWith(".html") || fileName.endsWith(".txt")
|| fileName.endsWith(".js")){
flag = true;
}
return flag;
}
/**
* 计算文件的行号
* @throws IOException
*/
public static void countNum(File file) throws IOException {
BufferedReader bufr = new BufferedReader(new FileReader(file));
String line = null;
while((line=bufr.readLine()) != null){
num++;
}
bufr.close();
}
/**
* 递归找到所有文件
* @throws IOException
*/
public static void find(File file) throws IOException {
if(file.isDirectory()){
File[] files = file.listFiles();
for(int i=0; i<files.length; i++){
find(files[i]);
}
} else if(file.isFile()){
String path = file.getAbsolutePath();
String fileName = file.getName();
if(isCode(fileName)){
countNum(file);
}
}
}
}
计算我写了多少行代码
最新推荐文章于 2022-09-21 19:43:15 发布