统计给定目录下,所有文件中的 行数、空格数、数字个数:
package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class FileStatic {
public static void main(String[] args) {
String path = "E:/file/myfile/msn/";
folderStatic(path);
}
/**
* 统计 某个目录下 每个文件的 行数、空格数、数字 个数,
* @param folderPath
*/
public static void folderStatic(String folderPath) {
try {
File folder = new File(folderPath);
System.out.println("folder [" + folderPath + "] exists? " + folder.exists());
File[] fList = folder.listFiles();
System.out.println("contains " + fList.length + " files");
for (int i = 0; i < fList.length; i++) {
int lineCount = 0, spaceCount = 0, numberCount = 0;
File _file = fList[i];
BufferedReader br = new BufferedReader(new FileReader(_file), 300);
String line = null;
while ((line = br.readLine()) != null) {
lineCount++;
for (int j = 0; j < line.length(); j++) {
char _c = line.charAt(j);
if (_c == ' ') { // 判断空格
spaceCount++;
} else if (_c > '0' && _c < '9') { // 判断 数字
numberCount++;
}
}
}
System.out.println("[" + _file.getName() + "]:\tline:" + lineCount + "\twhitespace:" + spaceCount + "\tnumber:" + numberCount);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}