之前因為需要統(tǒng)計一個項目的代碼行數(shù)胸哥,源代碼在不同目錄下材蹬,不好統(tǒng)計,所以寫了個遍歷指定目錄下所有文件夾文件統(tǒng)計行數(shù)的方法統(tǒng)計源代碼规惰。
private static int allCount = 0;
private static int fileNum = 0;
public static void countLine(File rootFile) {
File[] chileFileList = rootFile.listFiles();
for (File file : chileFileList) {
if(file.isDirectory()) {
countLine(file);
}else {
readCountLine(file);
}
}
}
public static void readCountLine(File file) {
fileNum++;
try {
Scanner sc = new Scanner(new FileInputStream(file));
int count = 0;
while(sc.hasNextLine()) {
count++;
sc.nextLine();
}
System.out.println(file.getAbsolutePath()+" : "+count+">>>第"+fileNum+"個文件");
allCount+=count;
sc.close();
} catch (FileNotFoundException e) {
System.out.println("nowCount = "+allCount+" "+"countFile : "+file.getAbsolutePath());
e.printStackTrace();
}
}