⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testlines.java

📁 远程电子考试系统
💻 JAVA
字号:
package countline;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class TestLines {
	private long sumLine = 0;
	public static final String[] KEYS={"package","import","//","{","}"};
public int countLOC(String line)
{
	if(line==null) return 0;
	line=line.trim(); 
	if(line.length()==0) return 0;
	else if(this.isStartWith(line))return 0;
	return 1;
}
public boolean isStartWith(String line){
	for(String key:KEYS){
		if(line.startsWith(key))return true;
	}
	return false;
}
	private void countLines(File sourceFolder) {
		if (sourceFolder.isFile() && sourceFolder.toString().endsWith("java")) {
			long counter=0;
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(sourceFolder)));
				String line="";
				boolean isInBlockComment=false;
				while ((line=br.readLine()) != null) 
		 {
				   line=line.trim();
				
					if(line.startsWith("/*")){
						isInBlockComment=true;
					}
					else if(line.endsWith("*/")){
						isInBlockComment=false;
					}
					if(!isInBlockComment){
					counter+=this.countLOC(line);
				}
				}
				br.close();
				sumLine+=counter;
				Date date=new Date(sourceFolder.lastModified());
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm ");
				System.out.println(sourceFolder.toString()+"\t\t||"+counter+"\t\t|| "+sf.format(date));
			
			} catch (FileNotFoundException ex) {
			} catch (IOException ex1) {	
			}
			
			
		}
		if (sourceFolder.isDirectory()) {
			File file[] = sourceFolder.listFiles();
			for (int i = 0; i < file.length; i++) {
				countLines(file[i]);
			}
		}
	}

	/**
	 * 这是对外的函数
	 * 
	 * @param f
	 * @return
	 */
	public long getSumLine(File f) {
		countLines(f);
		
		return sumLine;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -