?? testlines.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 + -