一个简单的代码统计器

下面是我写的一个非常简单的代码统计器,并没有考虑太多的情况。

 

package cn.lifx.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Count 
{
	public static void main(String[] args)
	{
		String path = "D:\\workspace\\test";
		
		Count count = new Count();
		
		System.out.println("Total line num is: " + count.countAllNum(path));
	}
	
	
	public int countAllNum(String path)
	{
		int num = 0;
		int temp = 0;
		
		ArrayList<String> list = getFiles(path);
		
		for(int i=0; i<list.size(); i++)
		{
			temp = countNum(list.get(i));
			
			num = num + temp;
		}
		
		return num;
	}
	
	public int countNum(String path)
	{
		int num = 0;
		
		BufferedReader br = null;
		
		try 
		{
			br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		if(br != null)
		{
			String line = "";
			
			try 
			{
				while(br.ready())
				{
					line = br.readLine();
					
					if(!line.equals(""))
					{
						num++;
					}
				}
				
				br.close();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return num;
	}
	
	public ArrayList<String> getFiles(String path)
	{
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String> temp = new ArrayList<String>();
		
		File f = new File(path);
		
		if(!f.exists())
		{
			System.out.println("The filepath " + path + " does not exist!");
		}
		else
		{
			if(f.isDirectory())
			{
				File[] files = f.listFiles();
				for(int i = 0; i < files.length; i++)
				{
					temp = getFiles(files[i].getAbsolutePath());
					for(String str : temp)
						list.add(str);
				}
			}
			else
			{
				if(path.endsWith(".java"))
				{
					String filepath = path.replace('\\','/');
					
					list.add(filepath);
				}
			}
		}

		return list;
	}
}

 

你可能感兴趣的:(java,F#)