WEB开发网
开发学院软件开发Java java 代码统计 阅读

java 代码统计

 2012-06-07 17:30:25 来源:WEB开发网   
核心提示:import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class CalCodeLine {/** * @author silencer * @throw
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;


public class CalCodeLine {

	/**
	 * @author silencer
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String filePath ="";
		File file = new File(filePath);
		System.out.println(" all code line :"+getFileLine(file));

	}
	
	/**
	 * 
	 * @param file
	 * @return  fileLine
	 * @throws IOException 
	 */
	public static int getFileLine(File file) throws IOException {
		int sFile = 0;
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (File tempFile : files) {
				if (tempFile.isDirectory()) {
					sFile += getFileLine(tempFile);
				} else {
					sFile += getJavaTextLength(tempFile);
				}
			}
		}else{
			sFile += getJavaTextLength(file);
		}
		return sFile;
	}
	/**
	 * 
	 * @param file
	 * @return textLine
	 * @throws IOException
	 */
	public static int getJavaTextLength(File file) throws IOException {
		int sLength = 0;
		if (file != null && file.getName().endsWith(".java")) {

			BufferedReader br = new BufferedReader(new FileReader(file));
			StringBuffer sb = new StringBuffer();
			sb.append(br.readLine());
			boolean isComment = false;
			boolean isSigleComment = false;

			while (!sb.toString().equals("null")) {
				
				if (sb.toString().startsWith("//")) {
					
					isSigleComment = true;
					
				} else if (sb.toString().contains("/*")) {
					
					isComment = true;
					
				} else if (sb.toString().contains("*/")) {
					
					isComment = false;
					sLength -= 1;
					
				}

				if (!isSigleComment && !isComment
						&& !sb.toString().startsWith("import")
						&& !sb.toString().isEmpty()) {
					sLength += 1;
				}
				isSigleComment = false;
				sb = sb.delete(0, sb.length());
				sb.append(br.readLine());
			}

			br.close();
		}
		
		return sLength;
	}

}

Tags:java 代码 统计

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接