java_获取多个文件夹下所有.java源码的总行数
Posted 李盘全
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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源码的总行数的主要内容,如果未能解决你的问题,请参考以下文章