import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class ItemCount
{
private int lineCount;
private int fileCount;
public int getLineCount()
{
return lineCount;
}
public int getFileCount()
{
return fileCount;
}
public static void main(String[] args) throws IOException
{
ItemCount itemCount = new ItemCount();
//path的值就是你的项目路径
String path = "<span style="color: rgb(0, 0, 255); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px; background-color: rgb(248, 248, 248);">C:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\123\\src\\StaticsCodeingLine.java</span>";
itemCount.getItemLineNum(new File(path));
System.out.println("该项目一共有"+itemCount.getFileCount()+"个java源文件,"+itemCount.getLineCount()+"行代码");
}
//递归
public void getItemLineNum(File path) throws IOException{
if(path.isFile() && path.getName().endsWith(".java")){
BufferedReader br = new BufferedReader(new FileReader(path));
fileCount++;
while(br.readLine()!=null){
lineCount++;
}
System.out.println(path.getName());
br.close();
}else if(path.isDirectory()){
File[] listFiles = path.listFiles();
for (File file : listFiles)
{
getItemLineNum(file);
}
}
}
}
作业1 目标2
最新推荐文章于 2024-06-21 19:20:27 发布