//package org.linecount.counter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
/**
* 计算一个项目中文件总行数
* @author Irfen
* @version 1.0.0
*/
public class LineCounter {
List<File> list = new ArrayList<File>();
int linenumber = 0;
FileReader fr = null;
BufferedReader br = null;
public void counter(String projectName) {
// String path = System.getProperty("user.dir");
String path = LineCounter.class.getResource("/").getPath(); // 同下个path
//具体项目路径因版本不同会有差异,下面会打印路径,请根据实际情况修改路径截取长度
path = path.substring(1, path.length() - 22) + "/" + projectName;
System.out.println(path);
File file = new File(path);
File files[] = null;
files = file.listFiles();
addFile(files);
isDirectory(files);
readLinePerFile();
System.out.println("Totle:" + linenumber + "行");
}
// 判断是否是目录
public void isDirectory(File[] files) {
for (File s : files) {
if (s.isDirectory()) {
File file[] = s.listFiles();
addFile(file);
isDirectory(file);
continue;
}
}
}
//将项目下所有文件组织成list
public void addFile(File file[]) {
for (int index = 0; index < file.length; index++) {
list.add(file[index]);
// System.out.println(list.size());
}
}
//读取所有非空白行
public void readLinePerFile() {
try {
for (File s : list) {
int yuan = linenumber;
if (s.isDirectory()) {
continue;
}
//扫描的文件类型
if(s.getName().indexOf(".jsp") != -1 ||
s.getName().indexOf(".java") != -1 ||
s.getName().indexOf(".htm") != -1 ||
s.getName().indexOf(".html") != -1 ||
s.getName().indexOf(".js") != -1 ||
s.getName().indexOf(".css") != -1 ) {
fr = new FileReader(s);
br = new BufferedReader(fr);
String i = "";
while ((i = br.readLine()) != null) {
if (!isBlankLine(i))//如果想加入空白行,请注释此行
linenumber++;
}
System.out.print(s.getName());
System.out.println("\t\t有" + (linenumber - yuan) + "行");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
}
}
if (fr != null) {
try {
fr.close();
} catch (Exception e) {
}
}
}
}
//是否是空行
public boolean isBlankLine(String i) {
if (i.trim().length() != 0) {
return false;
} else {
return true;
}
}
public static void main(String args[]) {
LineCounter lc = new LineCounter();
String projectName = "ufo"; //这里传入你的项目名称
lc.counter(projectName);
}
}
原文地址:http://irfen.iteye.com/blog/1185417
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
/**
* 计算一个项目中文件总行数
* @author Irfen
* @version 1.0.0
*/
public class LineCounter {
List<File> list = new ArrayList<File>();
int linenumber = 0;
FileReader fr = null;
BufferedReader br = null;
public void counter(String projectName) {
// String path = System.getProperty("user.dir");
String path = LineCounter.class.getResource("/").getPath(); // 同下个path
//具体项目路径因版本不同会有差异,下面会打印路径,请根据实际情况修改路径截取长度
path = path.substring(1, path.length() - 22) + "/" + projectName;
System.out.println(path);
File file = new File(path);
File files[] = null;
files = file.listFiles();
addFile(files);
isDirectory(files);
readLinePerFile();
System.out.println("Totle:" + linenumber + "行");
}
// 判断是否是目录
public void isDirectory(File[] files) {
for (File s : files) {
if (s.isDirectory()) {
File file[] = s.listFiles();
addFile(file);
isDirectory(file);
continue;
}
}
}
//将项目下所有文件组织成list
public void addFile(File file[]) {
for (int index = 0; index < file.length; index++) {
list.add(file[index]);
// System.out.println(list.size());
}
}
//读取所有非空白行
public void readLinePerFile() {
try {
for (File s : list) {
int yuan = linenumber;
if (s.isDirectory()) {
continue;
}
//扫描的文件类型
if(s.getName().indexOf(".jsp") != -1 ||
s.getName().indexOf(".java") != -1 ||
s.getName().indexOf(".htm") != -1 ||
s.getName().indexOf(".html") != -1 ||
s.getName().indexOf(".js") != -1 ||
s.getName().indexOf(".css") != -1 ) {
fr = new FileReader(s);
br = new BufferedReader(fr);
String i = "";
while ((i = br.readLine()) != null) {
if (!isBlankLine(i))//如果想加入空白行,请注释此行
linenumber++;
}
System.out.print(s.getName());
System.out.println("\t\t有" + (linenumber - yuan) + "行");
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
}
}
if (fr != null) {
try {
fr.close();
} catch (Exception e) {
}
}
}
}
//是否是空行
public boolean isBlankLine(String i) {
if (i.trim().length() != 0) {
return false;
} else {
return true;
}
}
public static void main(String args[]) {
LineCounter lc = new LineCounter();
String projectName = "ufo"; //这里传入你的项目名称
lc.counter(projectName);
}
}
原文地址:http://irfen.iteye.com/blog/1185417
本文介绍了一个用于统计项目中各种文件(如.jsp、.java等)非空白行数目的Java程序。该工具能够递归地遍历指定项目的文件夹结构,并针对特定类型的源代码文件进行行数统计。
1108

被折叠的 条评论
为什么被折叠?



