import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class TestWorkCode {
public static void main(String[] args) {
boolean b = false;
int whiteline = 0;//空白行
int workline = 0;//代码行
int commentline = 0;//注释行
String s = null;
BufferedReader br = null;
try {
br= new BufferedReader(new FileReader("D:\\eclipse_java\\TestIo\\src\\TestFile\\Test.java"));
while((s=br.readLine())!=null){
//s = s.trim();
if(s.matches("^[\\s&&[^\\n]]*$")){
whiteline++;
}else if(s.startsWith("//")){
commentline++;
}else if(s.startsWith("/*")&&!s.endsWith("*/")){//既然用startwith()就没必要用到 转义字符吧
/*
注意, 这个匹配还不完善, 例如如果我先缩进这个再写注释 这个匹配明显不合适,就像我这个注释一样, 楼主再好好想想吧
希望对你开阔思路有所帮助 ,我也是菜鸟一个
*/
b = true;
commentline++;
}else if(b ==true){
commentline++;
if(s.endsWith("*/")){
b = false;
}
}else{
workline++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br!=null){
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("whiteline"+whiteline+" "+"workline"+workline+" "+"commentline"+commentline);
}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;
public class TestWorkCode {
public static void main(String[] args) {
boolean b = false;
int whiteline = 0;//空白行
int workline = 0;//代码行
int commentline = 0;//注释行
String s = null;
BufferedReader br = null;
try {
br= new BufferedReader(new FileReader("D:\\eclipse_java\\TestIo\\src\\TestFile\\Test.java"));
while((s=br.readLine())!=null){
//s = s.trim();
if(s.matches("^[\\s&&[^\\n]]*$")){
whiteline++;
}else if(s.startsWith("//")){
commentline++;
}else if(s.startsWith("/*")&&!s.endsWith("*/")){//既然用startwith()就没必要用到 转义字符吧
/*
注意, 这个匹配还不完善, 例如如果我先缩进这个再写注释 这个匹配明显不合适,就像我这个注释一样, 楼主再好好想想吧
希望对你开阔思路有所帮助 ,我也是菜鸟一个
*/
b = true;
commentline++;
}else if(b ==true){
commentline++;
if(s.endsWith("*/")){
b = false;
}
}else{
workline++;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br!=null){
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("whiteline"+whiteline+" "+"workline"+workline+" "+"commentline"+commentline);
}
}
package RegEx;import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;public class Count {
static long normalLine = 0;
static long whiteLine = 0;
static long commentLine = 0;
public static void main(String[] args) {
File f = new File("C:\\Users\\WHY\\Java");
File[] files = f.listFiles();
for(File child: files ) {
if(child.getName().matches(".*\\.java$"))
parse(child);
}
System.out.println("The normalLines is" + normalLine);
System.out.println("The whiteLines is" + whiteLine);
System.out.println("The commentLines is" + commentLine);
} private static void parse(File child) {
BufferedReader br = null;
boolean comment = false;
try {
br = new BufferedReader(new FileReader(child));
String line = "";
while ((line = br.readLine())!= null) {
line = line.trim();//注意这句话是防止\t,该方法出去字符串两端的空格
if(line.matches("^[\\s&&[^\\n]]*$")){
whiteLine++;
}else if(line.startsWith("/*")&&!(line.endsWith("*/"))){
commentLine++;
comment = true;
}else if ((line.startsWith("/*"))&&(line.endsWith("*/"))) {
commentLine++;
}else if(comment){
commentLine++;
if(line.endsWith("*/")){
comment = false;
}
}else if (line.startsWith("//")){
commentLine++;
}
else {
normalLine++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用Eclipse编辑器,选择要统计的文件夹或工作目录,按Ctrl+H弹出搜索框,选择File Search页签,使用正则搜索,输入\n作为查询条件,文件匹配可以设置为*.jsp或*.java等。Scope选择为Selected Resource,然后点击查询即可统计出多少行代码了。