java统计你一共写了多少代码

版权声明:[email protected] https://blog.csdn.net/weixin_43198122/article/details/84865622

MyEclipse统计你一共写了多少代码(主要用io流实现的):

package study02;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class TestCountDir {
	private int count;
	
	private void countLine(File sourceFile) throws IOException{
		BufferedReader br=null;
		try{
			//新建文件输入流
			br=new BufferedReader(new FileReader(sourceFile));
			while(br.readLine()!=null){
				count++;
			}
		}finally{
			br.close();
		}
	}
	private void countDir(String sourceDir) throws IOException{
		File fSourceDir=new File(sourceDir);
		if(!fSourceDir.exists()||!fSourceDir.isDirectory()){
			System.out.println("源目录不存在!!!");
			return;
		}
		//遍历目录下的文件或目录
		File[] file=fSourceDir.listFiles();
		for(int i=0;i<file.length;i++){
			if(file[i].isFile()){
				if(file[i].getName().toLowerCase().endsWith(".java")){
					countLine(file[i]);
				}
			}
			if(file[i].isDirectory()){
				String subSourceDir=sourceDir+File.separator+file[i].getName();
				countDir(subSourceDir);
			}
		}
	}
	public static void main(String[] args) throws IOException {
		TestCountDir tcd=new TestCountDir();
		tcd.countDir("C:\\Users\\john20\\Workspaces\\MyEclipse 2017 CI\\mystudy");
		System.out.println(tcd.count);
	}
}

在这里插入图片描述

我从学习到现在才打这么点,嘿嘿。

猜你喜欢

转载自blog.csdn.net/weixin_43198122/article/details/84865622
今日推荐