package com.nange.file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 統計java檔案代碼行數
* 作者: 一天
* 時間:2018-04-08 17:19:56
*
*/
public class FileRowTotal {
/**
* 統計java檔案代碼行數
* 作者: 一天
* 時間:2018年4月8日 下午5:22:23
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String path="G:\\eclipseWorkSpace\\develop2\\code\\spdb_cm";
List<File> list=total(path);
System.out.println("檔案數量:"+list.size());
//統計代碼行數
Integer row=0;
for (File file : list) {
System.out.println(file.getName());
FileReader fr=new FileReader(file);//建立檔案輸入流
BufferedReader in=new BufferedReader(fr);//包裝檔案輸入流,可整行讀取
String line="";
while((line=in.readLine()) != null) {
row++;
}
}
System.out.println("代碼行數:"+row);
}
/**
* 擷取檔案的集合
* 作者: 一天
* 時間:2018年4月8日 下午5:20:36
* @param path
* @return
*/
public static List<File> total(String path){
List<File> fileList=new ArrayList<File>();
File file=new File(path);
File []files=file.listFiles();
if (files!=null&&files.length!=0) {
for (File file1 : files) {
if(file1.isFile()&&file1.toString().endsWith(".java")){
fileList.add(file1);
}else {
fileList.addAll(fileList.size(), total(file1.getPath()));
}
}
}
return fileList;
}
}