首页 > 代码库 > java_获取多个文件夹下所有.java源码的总行数
java_获取多个文件夹下所有.java源码的总行数
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class GetSourceCodeSumLine {
/**
* @param args
*/
public static void main(String[] args) {
// String JavaSourceCodeDir[] = {"G:\\workspace\\...\\...\\src", "G:\\workspace\\...\\...\\src"};
String JavaSourceCodeDir[] = { "E:\\workspace\\...\\...\\...\\src\\com" };
long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDir);
// ArrayList<String> JavaSourceCodeDirs = new ArrayList<String>();
//// JavaSourceCodeDirs.add("G:\\workspace\\...\\...\\src");
//// JavaSourceCodeDirs.add("G:\\workspace\\...\\...\\src");
// JavaSourceCodeDirs.add("E:\\workspace\\...\\...\\...\\src");
//
// long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDirs);
System.out.println(JavaSourceCodeDir.toString() + ":共有 "
+ codeCountLine + " 行java源代码");
}
public static long getJavaSourceCodeCountLine(String[] JavaSourceCodeDirs) {
long codeCountLine = 0;
for (String dirPath : JavaSourceCodeDirs) {
File fl = new File(dirPath);
if (fl.exists()) {
codeCountLine += getJavaFileCountLine(fl);
}
}
return codeCountLine;
}
public static long getJavaSourceCodeCountLine(ArrayList<String> JavaSourceCodeDirs) {
long codeCountLine = 0;
for (String dirPath : JavaSourceCodeDirs) {
File fl = new File(dirPath);
if (fl.exists()) {
codeCountLine += getJavaFileCountLine(fl);
}
}
return codeCountLine;
}
private static long getJavaFileCountLine(File fl) {
long javaCodeCountLine = 0;
if (fl != null && fl.exists()) {
if (fl.isDirectory()) {
File[] listFiles = fl.listFiles();
for (File file : listFiles) {
javaCodeCountLine += getJavaFileCountLine(file);
}
} else if (fl.isFile() && !fl.isHidden()) {
if (fl.getName().endsWith(".java")) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(fl);
br = new BufferedReader(fr);
while (br.readLine() != null)
javaCodeCountLine++;
br.close();
fr.close();
} catch (Exception e) {
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
fr = null;
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
br = null;
}
}
}
}
}
}
return javaCodeCountLine;
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class GetSourceCodeSumLine {
/**
* @param args
*/
public static void main(String[] args) {
// String JavaSourceCodeDir[] = {"G:\\workspace\\...\\...\\src", "G:\\workspace\\...\\...\\src"};
String JavaSourceCodeDir[] = { "E:\\workspace\\...\\...\\...\\src\\com" };
long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDir);
// ArrayList<String> JavaSourceCodeDirs = new ArrayList<String>();
//// JavaSourceCodeDirs.add("G:\\workspace\\...\\...\\src");
//// JavaSourceCodeDirs.add("G:\\workspace\\...\\...\\src");
// JavaSourceCodeDirs.add("E:\\workspace\\...\\...\\...\\src");
//
// long codeCountLine = getJavaSourceCodeCountLine(JavaSourceCodeDirs);
System.out.println(JavaSourceCodeDir.toString() + ":共有 "
+ codeCountLine + " 行java源代码");
}
public static long getJavaSourceCodeCountLine(String[] JavaSourceCodeDirs) {
long codeCountLine = 0;
for (String dirPath : JavaSourceCodeDirs) {
File fl = new File(dirPath);
if (fl.exists()) {
codeCountLine += getJavaFileCountLine(fl);
}
}
return codeCountLine;
}
public static long getJavaSourceCodeCountLine(ArrayList<String> JavaSourceCodeDirs) {
long codeCountLine = 0;
for (String dirPath : JavaSourceCodeDirs) {
File fl = new File(dirPath);
if (fl.exists()) {
codeCountLine += getJavaFileCountLine(fl);
}
}
return codeCountLine;
}
private static long getJavaFileCountLine(File fl) {
long javaCodeCountLine = 0;
if (fl != null && fl.exists()) {
if (fl.isDirectory()) {
File[] listFiles = fl.listFiles();
for (File file : listFiles) {
javaCodeCountLine += getJavaFileCountLine(file);
}
} else if (fl.isFile() && !fl.isHidden()) {
if (fl.getName().endsWith(".java")) {
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(fl);
br = new BufferedReader(fr);
while (br.readLine() != null)
javaCodeCountLine++;
br.close();
fr.close();
} catch (Exception e) {
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
fr = null;
}
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
br = null;
}
}
}
}
}
}
return javaCodeCountLine;
}
}
java_获取多个文件夹下所有.java源码的总行数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。